1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.mindtree.techworks.insight.gui.preferences;
25
26 import java.awt.Component;
27 import java.awt.GridBagConstraints;
28 import java.awt.GridBagLayout;
29 import java.awt.Insets;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.Iterator;
35
36 import javax.swing.AbstractListModel;
37 import javax.swing.BorderFactory;
38 import javax.swing.JButton;
39 import javax.swing.JFileChooser;
40 import javax.swing.JLabel;
41 import javax.swing.JList;
42 import javax.swing.JPanel;
43 import javax.swing.JScrollPane;
44 import javax.swing.JTextField;
45 import javax.swing.ListSelectionModel;
46
47 import com.mindtree.techworks.insight.InsightConstants;
48 import com.mindtree.techworks.insight.gui.widgets.StatusBar;
49 import com.mindtree.techworks.insight.preferences.PreferenceManager;
50 import com.mindtree.techworks.insight.preferences.model.Preference;
51 import com.mindtree.techworks.insight.preferences.model.PreferenceAttribute;
52 import com.mindtree.techworks.insight.preferences.model.PreferenceAttributeType;
53 import com.mindtree.techworks.insight.preferences.model.PreferenceInfo;
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class LocalFilesUIPanel extends AbstractPreferencesUIPanel implements
68 ActionListener {
69
70
71
72
73 private static final long serialVersionUID = 7653074890889562083L;
74
75
76
77
78
79 private static final String URL_PREFIX = "urlId";
80
81
82
83
84
85 private static final int NONE = 0;
86 private static final int ADD = 1;
87 private static final int EDIT = 2;
88 private static final int REMOVE = 3;
89
90
91
92
93
94
95 private final JFileChooser fileChooser = new JFileChooser(System.getProperty(InsightConstants.INSIGHT_HOME)); {
96 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
97 fileChooser.setMultiSelectionEnabled(true);
98 fileChooser.setDialogTitle(InsightConstants.getLiteral("SELECT_FILES"));
99 fileChooser.setApproveButtonText(InsightConstants.getLiteral("SELECT_FILES"));
100 }
101
102
103
104
105 private int action = NONE;
106
107
108
109
110 private JList configuredFilesetsList;
111 private JButton addButton;
112 private JButton browseButton;
113 private JButton editButton;
114 private JButton removeButton;
115 private JTextField nameField;
116 private JList urlList;
117 private JTextField newURLField;
118 private JButton addURLButton;
119 private JButton removeURLButton;
120 private JButton applyChangesButton;
121
122
123
124
125 private GridBagLayout gl;
126 private GridBagConstraints gc;
127
128
129
130
131
132
133
134
135
136 public LocalFilesUIPanel() {
137
138 }
139
140
141
142
143
144
145
146
147
148
149
150 private boolean isFileAlreadyExistsInFileSet(URLListModel urlListModel, String filePath){
151 for(int j = 0; j < urlListModel.getSize(); j++){
152 if(((String)(urlListModel.getElementAt(j))).equals(filePath)){
153 StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("FILE_ALREADY_LOADED"), false);
154 return true;
155 }
156 }
157 return false;
158 }
159
160
161
162
163
164 public void actionPerformed(ActionEvent event) {
165 Object source = event.getSource();
166 if (source == this.addButton) {
167 this.action = ADD;
168 this.configuredFilesetsList.removeSelectionInterval(0,this.configuredFilesetsList.getModel().getSize() - 1);
169 setDetailsWidgetsEnable(true);
170 } else if (source == this.editButton) {
171 if (this.configuredFilesetsList.getSelectedIndex() > -1) {
172 this.action = EDIT;
173 PreferenceInfo prefInfo = (PreferenceInfo)this.configuredFilesetsList
174 .getModel().getElementAt(this.configuredFilesetsList
175 .getSelectedIndex());
176 displayFilesetInfoForEdit(PreferenceManager.getInstance().getPreference(prefInfo.getCompleteId()));
177 }
178 } else if (source == this.removeButton) {
179 if (this.configuredFilesetsList.getSelectedIndex() > -1) {
180 this.action = REMOVE;
181 ((FilesetModel)this.configuredFilesetsList.getModel())
182 .removeFileset(this.configuredFilesetsList.getSelectedIndex());
183 setDetailsWidgetsEnable(false);
184 }
185 } else if (source == this.removeURLButton) {
186 if (this.urlList.getSelectedIndex() > -1) {
187 ((URLListModel)this.urlList.getModel())
188 .removeURL(this.urlList.getSelectedIndex());
189 }
190 } else if (source == this.addURLButton) {
191 if (newURLField.getText().trim().length() > 0) {
192 URLListModel urlListModel = (URLListModel)this.urlList.getModel();
193 if(!isFileAlreadyExistsInFileSet(urlListModel,newURLField.getText()))
194 urlListModel.addURL(newURLField.getText());
195 else
196 StatusBar.getInstance().setDisplayText(0, InsightConstants.getLiteral("FILE_ALREADY_LOADED"), false);
197 newURLField.setText("");
198 }
199 } else if (source == this.browseButton) {
200 if (fileChooser.showOpenDialog(this.parent) == JFileChooser.APPROVE_OPTION) {
201 File[] chosenFiles = fileChooser.getSelectedFiles();
202 for (int i = 0; i < chosenFiles.length; i++) {
203
204 URLListModel urlListModel = ((URLListModel)this.urlList.getModel());
205 String filePath = chosenFiles[i].getAbsolutePath();
206 if(!isFileAlreadyExistsInFileSet(urlListModel, filePath))
207 urlListModel.addURL(chosenFiles[i].getAbsolutePath());
208 else
209 StatusBar.getInstance().setDisplayText(0, InsightConstants.getLiteral("FILE_ALREADY_LOADED"), false);
210 }
211 }
212 } else if (source == applyChangesButton) {
213 if (this.nameField.getText().trim().length() < InsightConstants.PREFERENCE_NAME_MIN_SIZE) {
214 StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("ERROR_FILESET_NAME") +
215 InsightConstants.PREFERENCE_NAME_MIN_SIZE, false);
216 return;
217 }
218 Preference childPreference = null;
219 switch(this.action) {
220 case ADD:
221 childPreference = new Preference(String.valueOf(new Object().hashCode()), true, true, this.nameField.getText());
222 this.preference.addChildPreference(childPreference);
223
224
225 ((FilesetModel)this.configuredFilesetsList.getModel()).addFileset(childPreference);
226
227 break;
228 case EDIT:
229 childPreference = ((FilesetModel)this.configuredFilesetsList.
230 getModel()).getPreference(this.configuredFilesetsList.getSelectedIndex());
231 childPreference.setName(this.nameField.getText());
232 ((FilesetModel)this.configuredFilesetsList.getModel()).listDataUpdated(this.configuredFilesetsList.getSelectedIndex());
233 break;
234 }
235
236 updateURLListToPreference(childPreference);
237 setDetailsWidgetsEnable(false);
238 this.action = NONE;
239 }
240 }
241
242
243
244
245
246
247
248 protected void initializeDisplay() {
249 this.configuredFilesetsList = new JList(new FilesetModel());
250 this.configuredFilesetsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
251 addComponent(new JLabel(InsightConstants.getLiteral("CONFIGURED_FILESETS")),0,0,0,0,1,1);
252 addComponent(new JScrollPane(configuredFilesetsList),1,0,1,1,1,4);
253 this.addButton = new JButton(InsightConstants.getLiteral("ADD"));
254 this.editButton = new JButton(InsightConstants.getLiteral("EDIT"));
255 this.removeButton = new JButton(InsightConstants.getLiteral("REMOVE"));
256 this.addButton.addActionListener(this);
257 this.editButton.addActionListener(this);
258 this.removeButton.addActionListener(this);
259 addComponent(addButton,2,0,0,0,1,1);
260 addComponent(editButton,2,1,0,0,1,1);
261 addComponent(removeButton,2,2,0,0,1,1);
262
263
264 JPanel filesetDetailsPanel = new JPanel();
265 this.gl = new GridBagLayout();
266 this.gc = new GridBagConstraints();
267 filesetDetailsPanel.setLayout(gl);
268 filesetDetailsPanel.setBorder(BorderFactory.createTitledBorder(InsightConstants.getLiteral("FILESET_DETAILS")));
269 addComponent(filesetDetailsPanel,0,4,1,1,6,1);
270
271 addComponent(filesetDetailsPanel,new JLabel(InsightConstants.getLiteral("NAME")),0,0,0,0,1,1);
272 this.nameField = new JTextField();
273 addComponent(filesetDetailsPanel,nameField,1,0,1,0,2,1);
274 this.urlList = new JList(new URLListModel());
275 this.urlList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
276 addComponent(filesetDetailsPanel,new JLabel(InsightConstants.getLiteral("URLS")),0,1,0,0,1,1);
277 addComponent(filesetDetailsPanel,new JScrollPane(urlList),1,1,1,1,1,2);
278 this.removeURLButton = new JButton(InsightConstants.getLiteral("REMOVE"));
279 this.removeURLButton.addActionListener(this);
280 addComponent(filesetDetailsPanel,removeURLButton,3,1,0,0,1,1);
281 addComponent(filesetDetailsPanel,new JLabel(InsightConstants.getLiteral("NEW_URL")),0,3,0,0,1,1);
282 this.newURLField = new JTextField();
283 addComponent(filesetDetailsPanel,newURLField,1,3,1,0,2,1);
284 this.addURLButton = new JButton(InsightConstants.getLiteral("ADD"));
285 this.addURLButton.addActionListener(this);
286 addComponent(filesetDetailsPanel,addURLButton,3,3,0,0,1,1);
287 this.browseButton = new JButton(InsightConstants.getLiteral("BROWSE"));
288 this.browseButton.addActionListener(this);
289 addComponent(filesetDetailsPanel,browseButton,4,3,0,0,1,1);
290 this.applyChangesButton = new JButton(InsightConstants.getLiteral("APPLY_FILESET_CHANGES"));
291 this.applyChangesButton.addActionListener(this);
292 addComponent(filesetDetailsPanel,applyChangesButton,3,4,0,0,2,1);
293
294 setDetailsWidgetsEnable(false);
295 }
296
297
298
299
300
301
302 protected void setPreferenceValues() {
303
304 updateFilesetListToPreference();
305 }
306
307
308
309
310 private final void updateFilesetListToPreference() {
311 ArrayList oldValues = new ArrayList();
312 Iterator iterator = this.preference.iterateChildPreferenceIds();
313 while(iterator.hasNext()) {
314 oldValues.add(iterator.next());
315 }
316 iterator = oldValues.iterator();
317 while(iterator.hasNext()) {
318 this.preference.removePreference(
319 this.preference.getPreferenceById(
320 (String)iterator.next()));
321 }
322 for (int i = 0; i < this.configuredFilesetsList.getModel().getSize(); i++) {
323 this.preference.addChildPreference(((FilesetModel)this.configuredFilesetsList.getModel()).getPreference(i));
324 }
325 }
326
327
328
329
330
331 private final void updateURLListToPreference(Preference childPreference) {
332 ArrayList oldValues = new ArrayList();
333 Iterator iterator = childPreference.iteratePreferenceAttributeIds();
334 while(iterator.hasNext()) {
335 String id = (String)iterator.next();
336 if (id.startsWith(URL_PREFIX)) {
337 oldValues.add(id);
338 }
339 }
340 iterator = oldValues.iterator();
341 while(iterator.hasNext()) {
342 childPreference.removePreferenceAttribute(
343 childPreference.getPreferenceAttributeById(
344 (String)iterator.next()));
345 }
346 for (int i = 0; i < this.urlList.getModel().getSize(); i++) {
347 String identifier = (String)this.urlList.getModel().getElementAt(i);
348 PreferenceAttribute newAttribute = new PreferenceAttribute(PreferenceAttributeType.TEXT,
349 URL_PREFIX + i, identifier, false,
350 true, true, childPreference);
351 newAttribute.setName(URL_PREFIX + i);
352 childPreference.addPreferenceAttribute(newAttribute);
353 }
354 }
355
356
357
358
359
360
361 private final void displayFilesetInfoForEdit(Preference filesetInfo) {
362 setDetailsWidgetsEnable(true);
363 this.nameField.setText(filesetInfo.getName());
364 Iterator iterator = filesetInfo.iteratePreferenceAttributeIds();
365 while(iterator.hasNext()) {
366 String id = (String)iterator.next();
367 PreferenceAttribute prefAttribute = filesetInfo.getPreferenceAttributeById(id);
368 if (id.startsWith(URL_PREFIX)) {
369 ((URLListModel)this.urlList.getModel()).addURL(prefAttribute.getValue());
370 }
371 }
372 }
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395 private final void addComponent(JPanel panel, Component c, int gridx, int gridy, int weightx,
396 int weighty, int width, int height) {
397 gc.fill = GridBagConstraints.BOTH;
398 gc.anchor = GridBagConstraints.NORTHWEST;
399 gc.insets = new Insets(5,5,5,5);
400 gc.gridx = gridx;
401 gc.gridy = gridy;
402 gc.weightx = weightx;
403 gc.weighty = weighty;
404 gc.gridwidth = width;
405 gc.gridheight = height;
406 gl.setConstraints( c, gc);
407 panel.add(c);
408 }
409
410
411
412
413
414 private void setDetailsWidgetsEnable(boolean enable) {
415 this.nameField.setText("");
416 this.nameField.setEditable(enable);
417 this.nameField.setEnabled(enable);
418 ((URLListModel)this.urlList.getModel()).clearAll();
419 this.urlList.setEnabled(enable);
420 this.newURLField.setText("");
421 this.newURLField.setEditable(enable);
422 this.newURLField.setEnabled(enable);
423 this.removeURLButton.setEnabled(enable);
424 this.addURLButton.setEnabled(enable);
425 this.browseButton.setEnabled(enable);
426 this.applyChangesButton.setEnabled(enable);
427 if (enable) {
428 this.nameField.requestFocus();
429 }
430 }
431
432
433
434
435 private class FilesetModel extends AbstractListModel {
436
437
438
439
440 private static final long serialVersionUID = 1228554705949611510L;
441
442
443
444
445 private ArrayList configuredFilesets = new ArrayList();
446
447
448
449
450 public FilesetModel() {
451 Iterator iterator = preference.iterateChildPreferences();
452 while(iterator.hasNext()) {
453 Preference filesetPreference = (Preference)iterator.next();
454 configuredFilesets.add(filesetPreference);
455 }
456 }
457
458
459
460
461
462 public void addFileset(Preference filesetInfo) {
463 configuredFilesets.add(filesetInfo);
464 super.fireIntervalAdded(this, configuredFilesets.size() - 1, configuredFilesets.size() - 1);
465 }
466
467
468
469
470
471 public void listDataUpdated(int index) {
472 super.fireContentsChanged(this, index, index);
473 }
474
475
476
477
478
479 public void removeFileset(int index) {
480 if (index < 0 || index >= configuredFilesets.size()) {
481 return;
482 }
483 configuredFilesets.remove(index);
484 super.fireIntervalRemoved(this, index, index);
485 }
486
487
488
489
490
491
492 public int getSize() {
493 return configuredFilesets.size();
494 }
495
496
497
498
499
500
501 public Preference getPreference(int index) {
502 return (Preference)configuredFilesets.get(index);
503 }
504
505
506
507
508
509 public Object getElementAt(int index) {
510 return ((Preference)configuredFilesets.get(index)).getPreferenceInfo();
511 }
512 }
513
514
515
516
517 private class URLListModel extends AbstractListModel {
518
519
520
521
522 private static final long serialVersionUID = -1653119024030067594L;
523
524
525
526
527 private ArrayList configuredURLs = new ArrayList();
528
529
530
531
532 public URLListModel() {
533
534 }
535
536
537
538
539
540 public void addURL(String url) {
541 configuredURLs.add(url);
542 super.fireIntervalAdded(this, configuredURLs.size() - 1, configuredURLs.size() - 1);
543 }
544
545
546
547
548
549 public void removeURL(int index) {
550 if (index < 0 || index >= configuredURLs.size()) {
551 return;
552 }
553 configuredURLs.remove(index);
554 super.fireIntervalRemoved(this, index, index);
555 }
556
557
558
559
560 public void clearAll() {
561 if (configuredURLs.size() > 0) {
562 int lastIndex = configuredURLs.size() - 1;
563 configuredURLs.clear();
564 super.fireIntervalRemoved(this, 0, lastIndex);
565 }
566 }
567
568
569
570
571
572 public int getSize() {
573 return configuredURLs.size();
574 }
575
576
577
578
579
580 public Object getElementAt(int index) {
581 return configuredURLs.get(index);
582 }
583 }
584
585 }