diff --git a/src/main/java/plugins/adufour/ezplug/EzVar.java b/src/main/java/plugins/adufour/ezplug/EzVar.java index 8ed661c98b57e402fa15251fa27b0f5d4ebac14e..13975917010c9cf936925c7c2757eac19d5f5db3 100644 --- a/src/main/java/plugins/adufour/ezplug/EzVar.java +++ b/src/main/java/plugins/adufour/ezplug/EzVar.java @@ -137,7 +137,7 @@ public abstract class EzVar<T> extends EzComponent implements VarListener<T> * If no value is indicated, the target component will be visible as long as the * source component is both visible and enabled */ - public void addVisibilityTriggerTo(EzComponent targetComponent, T... values) + public void addVisibilityTriggerTo(EzComponent targetComponent, @SuppressWarnings("unchecked") T... values) { visibilityTriggers.put(targetComponent, values); diff --git a/src/main/java/plugins/adufour/ezplug/EzVarDoubleArray.java b/src/main/java/plugins/adufour/ezplug/EzVarDoubleArray.java index b50cc3b727b9ceb7bbeab1f0d39ceda399b3e421..cd777abe5f7efbff6229f884feb40675bf0f73a7 100644 --- a/src/main/java/plugins/adufour/ezplug/EzVarDoubleArray.java +++ b/src/main/java/plugins/adufour/ezplug/EzVarDoubleArray.java @@ -9,7 +9,6 @@ import plugins.adufour.vars.lang.VarDoubleArray; * @author Alexandre Dufour * */ -@SuppressWarnings("deprecation") @Deprecated public class EzVarDoubleArray extends EzVar<Double[]> { diff --git a/src/main/java/plugins/adufour/ezplug/EzVarIntegerArray.java b/src/main/java/plugins/adufour/ezplug/EzVarIntegerArray.java index a5752ea829c19ab36ae1a888ae59b1e69d0a1541..a58e95892af645a066b016636db3ba305562aa43 100644 --- a/src/main/java/plugins/adufour/ezplug/EzVarIntegerArray.java +++ b/src/main/java/plugins/adufour/ezplug/EzVarIntegerArray.java @@ -7,9 +7,7 @@ import plugins.adufour.vars.lang.VarIntegerArray; * * @deprecated use {@link EzVarIntegerArrayNative} instead (optimized performances) * @author Alexandre Dufour - * */ -@SuppressWarnings("deprecation") @Deprecated public class EzVarIntegerArray extends EzVar<Integer[]> { @@ -17,15 +15,16 @@ public class EzVarIntegerArray extends EzVar<Integer[]> * Creates a new integer variable with a given array of possible values * * @param varName - * the name of the variable (as it will appear on the interface) + * the name of the variable (as it will appear on the interface) * @param defaultValues - * the list of possible values the user may choose from + * the list of possible values the user may choose from * @param allowUserInput - * set to true to allow the user to input its own value manually, false otherwise + * set to true to allow the user to input its own value manually, false otherwise * @throws NullPointerException - * if the defaultValues parameter is null + * if the defaultValues parameter is null */ - public EzVarIntegerArray(String varName, Integer[][] defaultValues, boolean allowUserInput) throws NullPointerException + public EzVarIntegerArray(String varName, Integer[][] defaultValues, boolean allowUserInput) + throws NullPointerException { this(varName, defaultValues, 0, allowUserInput); } @@ -34,17 +33,18 @@ public class EzVarIntegerArray extends EzVar<Integer[]> * Creates a new integer variable with a given array of possible values * * @param varName - * the name of the variable (as it will appear on the interface) + * the name of the variable (as it will appear on the interface) * @param defaultValues - * the list of possible values the user may choose from + * the list of possible values the user may choose from * @param defaultValueIndex - * the index of the default selected value + * the index of the default selected value * @param allowUserInput - * set to true to allow the user to input its own value manually, false otherwise + * set to true to allow the user to input its own value manually, false otherwise * @throws NullPointerException - * if the defaultValues parameter is null + * if the defaultValues parameter is null */ - public EzVarIntegerArray(String varName, Integer[][] defaultValues, int defaultValueIndex, boolean allowUserInput) throws NullPointerException + public EzVarIntegerArray(String varName, Integer[][] defaultValues, int defaultValueIndex, boolean allowUserInput) + throws NullPointerException { super(new VarIntegerArray(varName, null), defaultValues, defaultValueIndex, allowUserInput); } diff --git a/src/main/java/plugins/adufour/vars/gui/swing/ComboBox.java b/src/main/java/plugins/adufour/vars/gui/swing/ComboBox.java index e862104a800d54840c9a39ffbe2f169084472ec2..6300513c097c6127ce60044c105ecfc786dc2cda 100644 --- a/src/main/java/plugins/adufour/vars/gui/swing/ComboBox.java +++ b/src/main/java/plugins/adufour/vars/gui/swing/ComboBox.java @@ -27,22 +27,22 @@ import plugins.adufour.vars.lang.Var; public class ComboBox<T> extends SwingVarEditor<T> { private ActionListener actionListener; - + /** * Creates a new combo box component for the specified variable. Note that the specified * variable must have a constraint of type {@link ValueSelectionModel} * * @param variable - * the variable to attach to this component + * the variable to attach to this component * @throws IllegalArgumentException - * if the variable has no constraint, or if the constraint is not a - * {@link ValueSelectionModel} + * if the variable has no constraint, or if the constraint is not a + * {@link ValueSelectionModel} */ public ComboBox(Var<T> variable) throws IllegalArgumentException { super(variable); } - + private static String arrayToString(Object array) { String s; @@ -52,15 +52,16 @@ public class ComboBox<T> extends SwingVarEditor<T> s += " " + Array.get(array, i).toString(); return s; } - + + @SuppressWarnings("unchecked") public JComponent createEditorComponent() throws IllegalArgumentException { VarEditorModel<T> cons = variable.getDefaultEditorModel(); - + List<T> defaultValues = null; boolean freeInput = true; T defaultValue = variable.getDefaultValue(); - + if (cons != null) { if (cons instanceof ValueSelectionModel) @@ -70,63 +71,77 @@ public class ComboBox<T> extends SwingVarEditor<T> freeInput = constraint.isFreeInput(); defaultValue = constraint.getDefaultValue(); } - else throw new IllegalArgumentException("Variable " + variable.getName() + " must have a value-type constraint"); + else + throw new IllegalArgumentException( + "Variable " + variable.getName() + " must have a value-type constraint"); + } + final JComboBox<T> jComboBox; + if (defaultValues == null) + { + jComboBox = new JComboBox<T>(); + } + else + { + jComboBox = new JComboBox<T>((T[]) defaultValues.toArray()); + //jComboBox = new JComboBox<T>(defaultValues.toArray()); } - - final JComboBox jComboBox = (defaultValues == null) ? new JComboBox() : new JComboBox(defaultValues.toArray()); jComboBox.setEditable(freeInput); jComboBox.setSelectedItem(defaultValue); - + actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { - if (variable.getReference() == null) updateVariableValue(); + if (variable.getReference() == null) + updateVariableValue(); } }; - + // Override the default renderer to support array-type items jComboBox.setRenderer(createRenderer()); - + // if the combo box allows user input, override the editor to support array-type items - if (jComboBox.isEditable()) jComboBox.setEditor(createEditor()); - + if (jComboBox.isEditable()) + jComboBox.setEditor(createEditor()); + return jComboBox; } - + protected ListCellRenderer<T> createRenderer() { return new ListCellRenderer<T>() { @Override - public Component getListCellRendererComponent(JList<? extends T> list, T value, int index, boolean isSelected, boolean cellHasFocus) + public Component getListCellRendererComponent(JList<? extends T> list, T value, int index, + boolean isSelected, boolean cellHasFocus) { String s = ""; if (value != null) { if (value.getClass().isArray()) s = arrayToString(value); - else s = value.toString(); + else + s = value.toString(); } return new JLabel(s); } }; } - + protected ComboBoxEditor createEditor() { return new ComboBoxEditor() { - final JTextField jTextField = new JTextField(); - final Color defaultColor = jTextField.getForeground(); - final Color errorColor = Color.red; - + final JTextField jTextField = new JTextField(); + final Color defaultColor = jTextField.getForeground(); + final Color errorColor = Color.red; + @Override public void addActionListener(ActionListener l) { - + } - + @Override public Component getEditorComponent() { @@ -136,12 +151,12 @@ public class ComboBox<T> extends SwingVarEditor<T> jTextField.setPreferredSize(dim); return jTextField; } - + @Override public T getItem() { T item = null; - + try { item = variable.parse(jTextField.getText()); @@ -154,22 +169,22 @@ public class ComboBox<T> extends SwingVarEditor<T> jTextField.setForeground(errorColor); jTextField.setToolTipText("Cannot parse input into a " + getVariable().getClass().getSimpleName()); } - + return item; } - + @Override public void removeActionListener(ActionListener l) { - + } - + @Override public void selectAll() { jTextField.selectAll(); } - + @Override public void setItem(Object item) { @@ -184,7 +199,7 @@ public class ComboBox<T> extends SwingVarEditor<T> } }; } - + /** * Replaces the list of values available in the combo box of this variable<br> * NOTE: this method will replace the current constraint on the variable @@ -211,43 +226,43 @@ public class ComboBox<T> extends SwingVarEditor<T> } }); } - + @SuppressWarnings("unchecked") protected void updateVariableValue() { variable.setValue((T) getEditorComponent().getSelectedItem()); } - + @Override protected void updateInterfaceValue() { getEditorComponent().getModel().setSelectedItem(variable.getValue()); } - + @Override public void dispose() { super.dispose(); - + // replace custom instances by new empty ones for garbage collection final JComboBox<T> jComboBox = getEditorComponent(); jComboBox.setRenderer(new DefaultListCellRenderer()); jComboBox.setModel(new DefaultComboBoxModel<T>()); } - + @SuppressWarnings("unchecked") @Override public JComboBox<T> getEditorComponent() { return (JComboBox<T>) super.getEditorComponent(); } - + @Override protected void activateListeners() { getEditorComponent().addActionListener(actionListener); } - + @Override protected void deactivateListeners() { diff --git a/src/main/java/plugins/adufour/vars/gui/swing/Spinner.java b/src/main/java/plugins/adufour/vars/gui/swing/Spinner.java index 7bb38c3020a8b1fa5479b582e548a26ff795d7b9..14ed6015a0b6386f34411d5ac6c81be49376691d 100644 --- a/src/main/java/plugins/adufour/vars/gui/swing/Spinner.java +++ b/src/main/java/plugins/adufour/vars/gui/swing/Spinner.java @@ -47,8 +47,7 @@ public class Spinner<N extends Number> extends SwingVarEditor<N> public void setValue(Object value) { super.setValue(value); - if (variable.getReference() == null) - variable.setValue((N) value); + variable.setValue((N) value); } }); diff --git a/src/main/java/plugins/adufour/vars/lang/VarArray.java b/src/main/java/plugins/adufour/vars/lang/VarArray.java index f7a43c7f1e1bd3c8f4271a401e651d237d378f53..ae9e936c89dee0a5e242ab682a45587832fc0203 100644 --- a/src/main/java/plugins/adufour/vars/lang/VarArray.java +++ b/src/main/java/plugins/adufour/vars/lang/VarArray.java @@ -15,9 +15,8 @@ import plugins.adufour.vars.util.VarListener; * use the {@link VarArray} instead * * @author Alexandre Dufour - * * @param <T> - * the inner type of the array + * the inner type of the array */ public class VarArray<T> extends VarGenericArray<T[]> implements Iterable<T> { @@ -25,51 +24,51 @@ public class VarArray<T> extends VarGenericArray<T[]> implements Iterable<T> * Creates a new array variable * * @param name - * the variable name + * the variable name * @param type - * the data type of the array (including the <code>[]</code>) + * the data type of the array (including the <code>[]</code>) * @param defaultValue - * the initial array + * the initial array */ public VarArray(String name, Class<T[]> type, T[] defaultValue) { super(name, type, defaultValue); } - + /** * Creates a new array variable * * @param name - * the variable name + * the variable name * @param type - * the data type of the array (including the <code>[]</code>) + * the data type of the array (including the <code>[]</code>) * @param defaultValue - * the initial array + * the initial array * @param defaultListener - * A listener to add to this variable immediately after creation + * A listener to add to this variable immediately after creation */ public VarArray(String name, Class<T[]> type, T[] defaultValue, VarListener<T[]> defaultListener) { super(name, type, defaultValue, defaultListener); } - + /** * Inserts the specified elements at the end of this array. This methods acts similarly to * {@link ArrayList#add(Object)}: a old array is replaced by a new array where the contents of * the old array is copied and the specified element is added last * * @param elements - * the elements to add + * the elements to add */ - public void add(T... elements) + public void add(@SuppressWarnings("unchecked") T... elements) { T[] oldArray = getValue(); ArrayList<T> newArray = new ArrayList<T>(size() + elements.length); - + for (T oldElem : oldArray) newArray.add(oldElem); newArray.addAll(Arrays.asList(elements)); - + setValue(newArray.toArray(oldArray)); }