View Javadoc

1   // %1117664849171:hoplugins.conv%
2   package hoplugins.conv;
3   
4   import java.awt.BorderLayout;
5   import java.awt.Container;
6   import java.awt.Dimension;
7   import java.awt.FlowLayout;
8   import java.awt.event.ActionEvent;
9   import java.awt.event.ActionListener;
10  
11  import javax.swing.ImageIcon;
12  import javax.swing.JButton;
13  import javax.swing.JDialog;
14  import javax.swing.JFrame;
15  import javax.swing.JPanel;
16  import javax.swing.JScrollPane;
17  import javax.swing.JTable;
18  
19  
20  /***
21   * DOCUMENT ME!
22   *
23   * @author Thorsten Dietz
24   */
25  public class SelectObjectDialog extends JDialog {
26      //~ Instance fields ----------------------------------------------------------------------------
27  
28      /*** TODO Missing Parameter Documentation */
29      protected JTable table;
30  
31      /*** TODO Missing Parameter Documentation */
32      protected String colOneName;
33  
34      /*** TODO Missing Parameter Documentation */
35      protected String colTwoName;
36      private Object[] object;
37      private Object[] selectedObjects = new Object[0];
38      private boolean defaultSelected;
39      private boolean isSaved = false;
40  
41      //~ Constructors -------------------------------------------------------------------------------
42  
43      /***
44       * Creates a new SelectObjectDialog object.
45       *
46       * @param frame TODO Missing Constructuor Parameter Documentation
47       * @param dialogWidth TODO Missing Constructuor Parameter Documentation
48       * @param dialogHeight TODO Missing Constructuor Parameter Documentation
49       * @param object TODO Missing Constructuor Parameter Documentation
50       */
51      public SelectObjectDialog(JFrame frame, int dialogWidth, int dialogHeight, Object[] object) {
52          super(frame, "", true);
53          this.object = object;
54          this.colOneName = "Export";
55          this.colTwoName = "HRF"; //colTwoName;
56          setLocation(frame.getX() + ((frame.getWidth() - dialogWidth) / 2),
57                      frame.getY() + ((frame.getHeight() - dialogHeight) / 2));
58          setSize(dialogWidth, dialogHeight);
59          inizialize();
60      }
61  
62      //~ Methods ------------------------------------------------------------------------------------
63  
64      /***
65       * TODO Missing Method Documentation
66       *
67       * @param b TODO Missing Method Parameter Documentation
68       */
69      protected void setDefaultSelected(boolean b) {
70          defaultSelected = b;
71      }
72  
73      /***
74       * TODO Missing Method Documentation
75       *
76       * @return TODO Missing Return Method Documentation
77       */
78      protected boolean isDefaultSelected() {
79          return defaultSelected;
80      }
81  
82      /***
83       * TODO Missing Method Documentation
84       *
85       * @param selected TODO Missing Method Parameter Documentation
86       * @param columnNames TODO Missing Method Parameter Documentation
87       *
88       * @return TODO Missing Return Method Documentation
89       */
90      protected SelectObjectTableModel getModel(boolean selected, String[] columnNames) {
91          Object[][] value = new Object[object.length][2];
92  
93          for (int i = 0; i < object.length; i++) {
94              value[i][0] = new Boolean(selected);
95              value[i][1] = object[i];
96          }
97  
98          SelectObjectTableModel model = new SelectObjectTableModel(value, columnNames);
99          return model;
100     }
101 
102     /***
103      * TODO Missing Method Documentation
104      *
105      * @return TODO Missing Return Method Documentation
106      */
107     protected boolean isSaved() {
108         return isSaved;
109     }
110 
111     /***
112      * TODO Missing Method Documentation
113      *
114      * @return TODO Missing Return Method Documentation
115      */
116     protected Object[] getSelectedObjects() {
117         return selectedObjects;
118     }
119 
120     /***
121      * TODO Missing Method Documentation
122      *
123      * @return TODO Missing Return Method Documentation
124      */
125     protected JTable getTable() {
126         return table;
127     }
128 
129     /***
130      * Erweitert Object-Array
131      *
132      * @param o TODO Missing Constructuor Parameter Documentation
133      */
134     protected void addSelectedObject(Object o) {
135         Object[] objectNew = new Object[selectedObjects.length + 1];
136 
137         for (int i = 0; i < selectedObjects.length; i++) {
138             objectNew[i] = selectedObjects[i];
139         }
140 
141         objectNew[selectedObjects.length] = o;
142         selectedObjects = objectNew;
143     }
144 
145     /***
146      * TODO Missing Method Documentation
147      */
148     protected void cancel() {
149         isSaved = false;
150         this.dispose();
151     }
152 
153     /***
154      * Speichert ausgewählte Objekte in einem Array
155      */
156     protected void findSelectedObjects() {
157         if (table != null) {
158             for (int i = 0; i < table.getRowCount(); i++) {
159                 boolean selected = ((Boolean) table.getValueAt(i, 0)).booleanValue();
160 
161                 if (selected) {
162                     addSelectedObject(table.getValueAt(i, 1));
163                 }
164             }
165 
166             isSaved = true;
167         }
168 
169         this.dispose();
170     }
171 
172     /***
173      * TODO Missing Method Documentation
174      *
175      * @return TODO Missing Return Method Documentation
176      */
177     private JPanel createButtons() {
178         JPanel buttonPanel = new JPanel();
179         ((FlowLayout) buttonPanel.getLayout()).setAlignment(FlowLayout.RIGHT);
180 
181         JButton okButton = new JButton(RSC.MINIMODEL.getResource().getProperty("Speichern"));
182         okButton.addActionListener(new ActionListener() {
183                 public void actionPerformed(ActionEvent event) {
184                     findSelectedObjects();
185                 }
186             });
187 
188         JButton cancelButton = new JButton(RSC.PROP_CANCEL);
189         cancelButton.addActionListener(new ActionListener() {
190                 public void actionPerformed(ActionEvent event) {
191                     cancel();
192                 }
193             });
194 
195         JButton selectAllButton = new JButton(new ImageIcon(SelectObjectDialog.class.getResource("rsc/CheckBoxSelected.gif")));
196         selectAllButton.setBorderPainted(false);
197         selectAllButton.setPreferredSize(new Dimension(18, 19));
198         selectAllButton.addActionListener(new ActionListener() {
199                 public void actionPerformed(ActionEvent event) {
200                     String[] columnNames = {colOneName, colTwoName};
201                     int[] columnWidth = {10, 100,};
202                     table.setModel(getModel(true, columnNames));
203 
204                     for (int i = 0; i < columnNames.length; i++) {
205                         table.getColumn(columnNames[i]).setPreferredWidth(columnWidth[i]);
206                     }
207 
208                     table.repaint();
209                 }
210             });
211 
212         JButton selectNoneButton = new JButton(new ImageIcon(SelectObjectDialog.class.getResource("rsc/CheckBoxNotSelected.gif")));
213         selectNoneButton.setBorderPainted(false);
214         selectNoneButton.setPreferredSize(new Dimension(18, 19));
215         selectNoneButton.addActionListener(new ActionListener() {
216                 public void actionPerformed(ActionEvent event) {
217                     String[] columnNames = {colOneName, colTwoName};
218                     int[] columnWidth = {10, 100,};
219                     table.setModel(getModel(false, columnNames));
220 
221                     for (int i = 0; i < columnNames.length; i++) {
222                         table.getColumn(columnNames[i]).setPreferredWidth(columnWidth[i]);
223                     }
224 
225                     table.repaint();
226                 }
227             });
228 
229         okButton.setPreferredSize(cancelButton.getPreferredSize());
230         buttonPanel.add(selectAllButton);
231         buttonPanel.add(selectNoneButton);
232         buttonPanel.add(okButton);
233         buttonPanel.add(cancelButton);
234         return buttonPanel;
235     }
236 
237     /***
238      * TODO Missing Method Documentation
239      *
240      * @return TODO Missing Return Method Documentation
241      */
242     private JScrollPane createTable() {
243         String[] columnNames = {colOneName, colTwoName};
244         int[] columnWidth = {10, 100,};
245 
246         table = new JTable(getModel(defaultSelected, columnNames));
247 
248         for (int i = 0; i < columnNames.length; i++) {
249             table.getColumn(columnNames[i]).setPreferredWidth(columnWidth[i]);
250         }
251 
252         JScrollPane scroll = new JScrollPane(table);
253         return scroll;
254     }
255 
256     /***
257      * TODO Missing Method Documentation
258      */
259     private void inizialize() {
260         Container contenPane = getContentPane();
261         contenPane.add(createTable());
262         contenPane.add(createButtons(), BorderLayout.SOUTH);
263     }
264 }