PageRenderTime 63ms CodeModel.GetById 36ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/ui/beans/LabeledCombobox.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 360 lines | 187 code | 44 blank | 129 comment | 13 complexity | df7ca48caf50fb30c066f2f7c8231be1 MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5
  6/*
  7 * LabeledTextField.java
  8 *
  9 * Created on 20.11.2008, 19:26:39
 10 */
 11package mpv5.ui.beans;
 12
 13import java.awt.Font;
 14import java.awt.event.ActionEvent;
 15import java.awt.event.ActionListener;
 16import java.util.Arrays;
 17import java.util.List;
 18import java.util.Vector;
 19import javax.swing.JComboBox;
 20import javax.swing.JTextArea;
 21import mpv5.db.common.Context;
 22import mpv5.db.common.DatabaseObject;
 23import mpv5.handler.MPEnum;
 24import mpv5.logging.Log;
 25import mpv5.ui.dialogs.subcomponents.DatabaseObejctReceiver;
 26import mpv5.ui.panels.DataPanel;
 27import mpv5.utils.models.*;
 28
 29/**
 30 *
 31 *  
 32 */
 33public class LabeledCombobox extends javax.swing.JPanel {
 34
 35    private static final long serialVersionUID = 1L;
 36    private String _text;
 37    private String _label;
 38
 39    /** Creates new form LabeledTextField */
 40    public LabeledCombobox() {
 41        initComponents();
 42    }
 43
 44    public JComboBox getComboBox() {
 45        return mPCombobox1.getComboBox();
 46    }
 47
 48    /**
 49     *
 50     * @return The model
 51     */
 52    public MPComboboxModel getModel() {
 53        return mPCombobox1.getModel();
 54    }
 55
 56    /**
 57     *
 58     * @param values
 59     */
 60    public void setModel(MPEnum[] values) {
 61        mPCombobox1.setModel(MPComboBoxModelItem.toModel(values));
 62    }
 63
 64    /**
 65     * Sets an empty model
 66     */
 67    public void setModel() {
 68        mPCombobox1.setModel();
 69    }
 70
 71    public void setModel(List<Context> values, boolean bloedsinn) {
 72        mPCombobox1.setModel(new MPComboboxModel(MPComboBoxModelItem.toItems(values, bloedsinn)));
 73    }
 74
 75    /**
 76     * 
 77     * @param values
 78     * @param compareMode
 79     * @param skip
 80     */
 81    public void setModel(MPEnum[] values, int compareMode, List<Integer> skip) {
 82        mPCombobox1.setModel(MPComboBoxModelItem.toModel(values, compareMode, skip));
 83    }
 84
 85    /**
 86     * Uses enum.name() as ID, and enum.toString() as value.
 87     * @param values
 88     */
 89    public void setModel(Enum[] values) {
 90        String[][] val = new String[values.length][2];
 91        for (int i = 0; i < values.length; i++) {
 92            Enum enum1 = values[i];
 93            val[i][0] = enum1.name();
 94            val[i][1] = enum1.toString();
 95        }
 96        setModel(val);
 97    }
 98
 99    /**
100     * Delegates to setModel(MPComboBoxModelItem.toModel(data));
101     * {id (hidden), value (shown in the list)}
102     * @param data
103     */
104    public void setModel(Object[][] data) {
105        mPCombobox1.setModel(MPComboBoxModelItem.toModel(data));
106    }
107
108    /**
109     *
110     * @return An {@link MPComboBoxModelItem} or null if nothing is selected. Attention - may return an item with ID -1 if a default item was set.
111     */
112    public MPComboBoxModelItem getSelectedItem() {
113        if (mPCombobox1.getSelectedItem() instanceof MPComboBoxModelItem) {
114            return mPCombobox1.getSelectedItem();
115        } else {
116            return null;
117        }
118    }
119
120    /**
121     * Set the model. Should contain only {@link MPComboBoxModelItem}s
122     * @param model
123     */
124    public void setModel(MPComboboxModel model) {
125        mPCombobox1.setModel(model);
126    }
127
128    /**
129     * Convenience Method to set a single {@link DatabaseObject} as the model of the combobox.<br/>
130     * Will set the DO as the selected item after adding.
131     * @param obj
132     */
133    public void setModel(DatabaseObject obj) {
134        setModel(new Vector<DatabaseObject>(Arrays.asList(new DatabaseObject[]{
135                    obj
136                })));
137        setSelectedIndex(0);
138    }
139
140    /**
141     * Convenience Method to set a {@link List} of {@link DatabaseObject}s as the model of the combobox.<br/>
142     * @param vector
143     */
144    public void setModel(List<DatabaseObject> vector) {
145        setModel(new MPComboboxModel(MPComboBoxModelItem.toItems(vector)));
146    }
147
148    /**
149     * Delegates to getComboBox().setSelectedIndex(itemID);
150     * @param itemindex
151     */
152    public void setSelectedIndex(int itemindex) {
153        if (getComboBox().getItemCount() > 0) {
154            if (itemindex < getComboBox().getItemCount()) {
155                getComboBox().setSelectedIndex(itemindex);
156            } else {
157                throw new IndexOutOfBoundsException(itemindex + " must be lower than " + getComboBox().getItemCount());
158            }
159        }
160    }
161
162    /**
163     * Sets the item with the given value as selected item
164     * @param valueOfItem
165     */
166    public void setSelectedItem(String valueOfItem) {
167        mPCombobox1.setSelectedIndex(MPComboBoxModelItem.getItemIDfromValue(valueOfItem, mPCombobox1.getModel()));
168    }
169
170    /**
171     * Sets the item with the given ID object as selected item
172     * @param ID
173     */
174    public void setSelectedItem(Object ID) {
175        mPCombobox1.setSelectedIndex(MPComboBoxModelItem.getItemID(ID, mPCombobox1.getModel()));
176    }
177
178    /**
179     * If set to true, hitting "Enter" on the text field will trigger a search for the entered value and popup the results if any.
180     * <br/>{@link LabeledCombobox#setContext(Context)} must be called before this can work.
181     * @param enabled
182     */
183    public void setSearchEnabled(boolean enabled) {
184        mPCombobox1.setSearchEnabled(enabled);
185    }
186
187    /**
188     * Set the context for database queries
189     * @param c
190     */
191    public void setContext(Context c) {
192        mPCombobox1.setContext(c);
193    }
194
195    /** This method is called from within the constructor to
196     * initialize the form.
197     * WARNING: Do NOT modify this code. The content of this method is
198     * always regenerated by the Form Editor.
199     */
200    @SuppressWarnings("unchecked")
201    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
202    private void initComponents() {
203
204        jLabel1 = new javax.swing.JLabel();
205        mPCombobox1 = new mpv5.ui.beans.MPCombobox();
206
207        setOpaque(false);
208        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));
209
210        jLabel1.setText("text");
211        jLabel1.setMaximumSize(new java.awt.Dimension(333, 333));
212        jLabel1.setPreferredSize(new java.awt.Dimension(100, 18));
213        add(jLabel1);
214
215        mPCombobox1.setBorder(null);
216        mPCombobox1.setMinimumSize(new java.awt.Dimension(33, 18));
217        add(mPCombobox1);
218    }// </editor-fold>//GEN-END:initComponents
219    // Variables declaration - do not modify//GEN-BEGIN:variables
220    private javax.swing.JLabel jLabel1;
221    private mpv5.ui.beans.MPCombobox mPCombobox1;
222    // End of variables declaration//GEN-END:variables
223
224    /**
225     * Gives a {@link MPComboBoxModelItem} or null if nothing is selected
226     * @return the combobox item
227     * @throws ClassCastException
228     */
229    public MPComboBoxModelItem getValue() {
230        return mPCombobox1.getSelectedItem();
231    }
232
233    /**
234     * The textual representation of the seleceted item or null if nothing is selected
235     * @return the text of the combobox
236     */
237    public String getText() {
238        String t = mPCombobox1.getComboBox().getEditor().getItem().toString();
239        if (t.equals("null")) {
240            return null;
241        } else {
242            return t;
243        }
244    }
245
246    /**
247     * @return the _label
248     */
249    public String get_Label() {
250        return jLabel1.getText();
251    }
252
253    /**
254     * @param label the _label to set
255     */
256    public void set_Label(String label) {
257        this._label = label;
258        jLabel1.setText(_label);
259        this.setToolTipText(_text);
260        jLabel1.setToolTipText(_text);
261    }
262
263    @Deprecated
264    public void set_LabelFont(Font font) {
265    }
266
267    @Override
268    public void setEnabled(boolean enabled) {
269        jLabel1.setEnabled(enabled);
270        mPCombobox1.setEnabled(enabled);
271    }
272
273    /**
274    Sets the selected item in the combo box display area to the object in the argument. If anObject is in the list, the display area shows anObject selected.
275    If anObject is not in the list and the combo box is uneditable, it will not change the current selection. For editable combo boxes, the selection will change to anObject.
276    If this constitutes a change in the selected item, ItemListeners added to the combo box will be notified with one or two ItemEvents. If there is a current selected item, an ItemEvent will be fired and the state change will be ItemEvent.DESELECTED. If anObject is in the list and is not currently selected then an ItemEvent will be fired and the state change will be ItemEvent.SELECTED.
277    ActionListeners added to the combo box will be notified with an ActionEvent when this method is called.
278    Parameters:
279    anObject - the list object to select; use null to clear the selection
280     * @param text
281     */
282    public void setValue(String text) {
283        mPCombobox1.setValue(text);
284    }
285
286    /**
287     * Trigger a search on the combo box
288     */
289    public void triggerSearch() {
290        mPCombobox1.search(true);
291    }
292
293    /**
294     * Define a textfield wich displays selection changes
295     * @param receiver 
296     */
297    public void setReceiver(final LabeledTextField receiver) {
298        getComboBox().addActionListener(new ActionListener() {
299
300            public void actionPerformed(ActionEvent e) {
301                receiver.setDisplayingObject(getSelectedItem());
302            }
303        });
304    }
305
306    /**
307     * 
308     * @param panel
309     */
310    public void setReceiver(DataPanel panel) {
311        mPCombobox1.setReceiver(panel);
312    }
313
314    /**
315     * 
316     * @param b
317     */
318    public void setSearchOnEnterEnabled(boolean b) {
319        setSearchEnabled(b);
320    }
321
322    /**
323     * Enable/disable editing of the combobox
324     * @param b
325     */
326    public void setEditable(boolean b) {
327        getComboBox().setEditable(b);
328    }
329
330    public void setReceiver(final JTextArea receiver) {
331        getComboBox().addActionListener(new ActionListener() {
332
333            public void actionPerformed(ActionEvent e) {
334                try {
335                    receiver.setText(getSelectedItem().toString());
336                } catch (Exception eg) {
337                }
338            }
339        });
340    }
341
342    public void setReceiver(final DatabaseObejctReceiver obj) {
343        getComboBox().addActionListener(new ActionListener() {
344
345            public void actionPerformed(ActionEvent e) {
346                try {
347                    if (getSelectedItem().getSelectedItem() instanceof DatabaseObject) {
348                        obj.receive((DatabaseObject) getSelectedItem().getSelectedItem());
349                    } else if (mPCombobox1.getContext() != null) {
350                        obj.receive(DatabaseObject.getObject(mPCombobox1.getContext(), (Integer) getSelectedItem().getIdObject()));
351                    } else {
352                        Log.Debug(this, "No dbos in model or nor context set!");
353                    }
354                } catch (Exception ex) {
355                    Log.Debug(this, ex.getMessage());
356                }
357            }
358        });
359    }
360}