KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > tracelog > ui > ShellOptionLogConfig


1 package net.sourceforge.tracelog.ui;
2
3 import java.io.File JavaDoc;
4 import java.util.LinkedList JavaDoc;
5 import java.util.List JavaDoc;
6
7 import net.sourceforge.tracelog.config.LogFile;
8 import net.sourceforge.tracelog.listeners.GenericListener;
9 import net.sourceforge.tracelog.utils.Util;
10
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.MouseEvent;
13 import org.eclipse.swt.graphics.Cursor;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Event;
19 import org.eclipse.swt.widgets.FileDialog;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.MessageBox;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.swt.widgets.TableColumn;
24 import org.eclipse.swt.widgets.TableItem;
25 import org.eclipse.swt.widgets.Text;
26
27 public class ShellOptionLogConfig extends AbstractWidget {
28     // private static Logger log = Logger.getLogger(ShellOptionLogConfig.class);
29

30     public static final int MAX_LOG_NAME_LENGTH = 15;
31     public static final int MAX_TABLE_ROW = 10;
32
33     private static final int COLUMN_LOG_NAME = 0;
34     private static final int COLUMN_LOG_FILE_PATH = 1;
35
36     private static final int DEFAULT_BACKGROUND_COLOR = SWT.COLOR_WHITE;
37     private static final int DEFAULT_FOREGROUND_COLOR = SWT.COLOR_BLACK;
38     private static final String JavaDoc DEFAULT_VALUE = "[available slot]";
39
40     private static final String JavaDoc[] TABLE_COLUMN_NAMES = {
41             "Log Name",
42             "Log File Path"
43                                                                 };
44
45     private static final int[] TABLE_COLUMN_WIDTHS = {
46             100,
47             480
48                                                                 };
49
50     private Label backgroundColorText;
51     private Button cancelBtn;
52     private Button deleteBtn;
53     private Composite entryFieldsComposite;
54     private Label foregroundColorText;
55     private List JavaDoc<LogFile> logFiles;
56     private Composite logConfigComposite;
57     private Text logFilePathText;
58     private Text logNameText;
59     private Button saveBtn;
60     private int selectedLogFileIndex;
61     private Table table;
62     private Label text;
63
64     /**
65      * Package-access constructor.
66      */

67     ShellOptionLogConfig() {
68         super();
69         this.logFiles = new LinkedList JavaDoc<LogFile>();
70         this.table = null;
71     }
72
73     /**
74      * Returns log bean list.
75      *
76      * @return Log bean list.
77      */

78     public List JavaDoc<LogFile> getLogFiles() {
79         return logFiles;
80     }
81
82     /**
83      * Controller to display the log config dialog.
84      */

85     public void run() {
86
87         mediator.handleEvent(ActionMediator.EVENT_SET_LOG_FILES);
88
89         logConfigComposite = new Composite(parentComposite, SWT.NONE);
90
91         logConfigComposite.setLayout(new GridLayout());
92         logConfigComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
93
94         widgetFactory.createLogConfigShellColorChooser(parentShell, logConfigComposite, mediator);
95
96         UIUtil.createTitle(logConfigComposite, "Log Files");
97
98         setupInstruction();
99         setupTable();
100         setupEntryFields();
101         setupButtons();
102
103         logConfigComposite.setSize(parentComposite.computeSize(parentComposite.getSize().x, SWT.DEFAULT));
104     }
105
106     /**
107      * Sets log files.
108      *
109      * @param logFiles
110      * Log files.
111      */

112     public void setLogFiles(List JavaDoc<LogFile> logFiles) {
113         this.logFiles = logFiles;
114     }
115
116     /**
117      * Deletes an entry field and updates the table content and the server log
118      * configuration.
119      */

120     private void deleteEntryField() {
121         // trying to delete an available entry
122
if (selectedLogFileIndex < logFiles.size()) {
123             logFiles.remove(selectedLogFileIndex);
124             mediator.handleEvent(ActionMediator.EVENT_SAVE_SERVER_LOG_CONFIGURATION);
125             table.clearAll();
126             displayTableContent();
127         }
128
129         hideEntryFields();
130     }
131
132     /**
133      * Prefills the entry field values, and then displays them and the buttons.
134      *
135      * @param foregroundColor
136      * Foreground color.
137      * @param backgroundColor
138      * Background color.
139      * @param logName
140      * Log name.
141      * @param logFilePath
142      * Log file path.
143      */

144     private void displayEntryFields(int foregroundColor, int backgroundColor, String JavaDoc logName, String JavaDoc logFilePath) {
145         foregroundColorText.setBackground(display.getSystemColor(foregroundColor));
146         backgroundColorText.setBackground(display.getSystemColor(backgroundColor));
147
148         foregroundColorText.setData(foregroundColor);
149         backgroundColorText.setData(backgroundColor);
150         logNameText.setText(logName);
151         logFilePathText.setText(logFilePath);
152
153         entryFieldsComposite.setVisible(true);
154         saveBtn.setVisible(true);
155         cancelBtn.setVisible(true);
156         deleteBtn.setVisible(true);
157     }
158
159     /**
160      * Displays the table content.
161      */

162     private void displayTableContent() {
163         for (int i = 0; i < MAX_TABLE_ROW; ++i) {
164             int backgroundColor = DEFAULT_BACKGROUND_COLOR;
165             int foregroundColor = DEFAULT_FOREGROUND_COLOR;
166             String JavaDoc logName = DEFAULT_VALUE;
167             String JavaDoc logFilePath = DEFAULT_VALUE;
168
169             if (i < logFiles.size()) {
170                 LogFile logFile = (LogFile) logFiles.get(i);
171                 backgroundColor = logFile.getBackgroundColor();
172                 foregroundColor = logFile.getForegroundColor();
173                 logName = logFile.getLogName();
174                 logFilePath = logFile.getLogPath();
175             }
176
177             setTableItem(i, foregroundColor, backgroundColor, logName, logFilePath);
178         }
179     }
180
181     /**
182      * Hides the entry fields and the buttons.
183      */

184     private void hideEntryFields() {
185         entryFieldsComposite.setVisible(false);
186
187         if (saveBtn != null && !saveBtn.isDisposed()) {
188             saveBtn.setVisible(false);
189         }
190
191         if (cancelBtn != null && !cancelBtn.isDisposed()) {
192             cancelBtn.setVisible(false);
193         }
194
195         if (deleteBtn != null && !deleteBtn.isDisposed()) {
196             deleteBtn.setVisible(false);
197         }
198     }
199
200     /**
201      * Performs validation on the entry fields before saving the configuration
202      * permanently.
203      */

204     private void saveConfig() {
205         String JavaDoc msg = "";
206
207         List JavaDoc<String JavaDoc> existingLogNames = new LinkedList JavaDoc<String JavaDoc>();
208
209         // get all the existing log names excluding the selected entry
210
for (int i = 0; i < logFiles.size(); ++i) {
211             if (i != selectedLogFileIndex) {
212                 existingLogNames.add(logFiles.get(i).getLogName());
213             }
214         }
215
216         // int rowNum = selectedLogFileIndex + 1;
217
int foregroundColor = (Integer JavaDoc) foregroundColorText.getData();
218         int backgroundColor = (Integer JavaDoc) backgroundColorText.getData();
219         String JavaDoc logName = logNameText.getText();
220         String JavaDoc logFilePath = logFilePathText.getText();
221
222         // one (or more) field is empty
223
if (Util.isEmpty(logName) || Util.isEmpty(logFilePath)) {
224             msg += "\n- Log name and log file path must all exist.";
225         }
226         // log name has invalid characters
227
else if (!logName.matches("[A-Za-z0-9_ ]*")) {
228             msg += "\n- Log name has invalid character(s). Please use only alphanumeric, underscore and space.";
229         }
230         // log name already exists
231
else if (existingLogNames.contains(logName)) {
232             msg += "\n- Log name already exists. Make sure all log names are unique.";
233         }
234         // file does not exist
235
else if (!new File JavaDoc(logFilePath).exists()) {
236             msg += "\n- This log file path either does not exist or you do not have sufficient privilege to access it.";
237         }
238         // not a file
239
else if (!new File JavaDoc(logFilePath).isFile()) {
240             msg += "\n- This log file path does not represent a file.";
241         }
242         // no errors
243
else {
244             LogFile logFile = null;
245             int newTableItemIndex = 0;
246
247             if (selectedLogFileIndex < logFiles.size()) {
248                 newTableItemIndex = selectedLogFileIndex;
249                 logFile = logFiles.get(selectedLogFileIndex);
250                 logFile.setBackgroundColor(backgroundColor);
251                 logFile.setForegroundColor(foregroundColor);
252                 logFile.setLogName(logName);
253                 logFile.setLogPath(logFilePath);
254             }
255             else {
256                 newTableItemIndex = logFiles.size();
257                 logFile = new LogFile(newTableItemIndex + 1, logName, logFilePath, foregroundColor, backgroundColor);
258                 logFiles.add(logFile);
259             }
260
261             mediator.handleEvent(ActionMediator.EVENT_SAVE_SERVER_LOG_CONFIGURATION);
262             setTableItem(newTableItemIndex, foregroundColor, backgroundColor, logName, logFilePath);
263             hideEntryFields();
264
265             return;
266         }
267
268         // will reach here if there is error
269
MessageBox mb = new MessageBox(parentShell, SWT.OK | SWT.ICON_ERROR);
270         mb.setText("Log Files Dialog");
271         mb.setMessage("Please fix the following error(s):\n" + msg);
272         mb.open();
273     }
274
275     /**
276      * Sets up a table item.
277      *
278      * @param tableItemIndex
279      * Position of the table item.
280      * @param foregroundColor
281      * Foreground color.
282      * @param backgroundColor
283      * Background color.
284      * @param logName
285      * Log name.
286      * @param logFilePath
287      * Log file path.
288      */

289     private void setTableItem(int tableItemIndex, int foregroundColor, int backgroundColor, String JavaDoc logName, String JavaDoc logFilePath) {
290         TableItem tableItem = null;
291
292         if (tableItemIndex < table.getItemCount()) {
293             tableItem = table.getItem(tableItemIndex);
294         }
295         else {
296             tableItem = new TableItem(table, SWT.NONE);
297         }
298
299         tableItem.setText(COLUMN_LOG_NAME, logName);
300         tableItem.setText(COLUMN_LOG_FILE_PATH, logFilePath);
301         tableItem.setForeground(display.getSystemColor(foregroundColor));
302         tableItem.setBackground(display.getSystemColor(backgroundColor));
303
304         LogFile logFile = new LogFile(tableItemIndex, logName, logFilePath, foregroundColor, backgroundColor);
305         tableItem.setData(logFile);
306     }
307
308     /**
309      * Sets up action buttons: save, cancel, delete and close.
310      */

311     private void setupButtons() {
312         Composite c = new Composite(logConfigComposite, SWT.NONE);
313         c.setLayout(new GridLayout(5, true));
314         c.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
315
316         Label label = new Label(c, SWT.NONE);
317         GridData spacerGD = new GridData(GridData.FILL_HORIZONTAL);
318         spacerGD.horizontalSpan = 2;
319         label.setLayoutData(spacerGD);
320
321         GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
322
323         saveBtn = new Button(c, SWT.PUSH);
324         saveBtn.setText("Save");
325         saveBtn.setVisible(false);
326         saveBtn.setLayoutData(gridData);
327
328         saveBtn.addMouseListener(new GenericListener() {
329             public void mouseUp(MouseEvent e) {
330                 saveConfig();
331             }
332         });
333
334         cancelBtn = new Button(c, SWT.PUSH);
335         cancelBtn.setText("Cancel");
336         cancelBtn.setVisible(false);
337         cancelBtn.setLayoutData(gridData);
338
339         cancelBtn.addMouseListener(new GenericListener() {
340             public void mouseUp(MouseEvent e) {
341                 hideEntryFields();
342             }
343         });
344
345         deleteBtn = new Button(c, SWT.PUSH);
346         deleteBtn.setText("Delete");
347         deleteBtn.setVisible(false);
348         deleteBtn.setLayoutData(gridData);
349
350         deleteBtn.addMouseListener(new GenericListener() {
351             public void mouseUp(MouseEvent e) {
352                 deleteEntryField();
353             }
354         });
355
356         // Button closeBtn = new Button(c, SWT.PUSH);
357
// closeBtn.setText("Close");
358
// closeBtn.setFocus();
359
// closeBtn.setLayoutData(gridData);
360
//
361
// closeBtn.addMouseListener(new GenericListener() {
362
// public void mouseUp(MouseEvent e) {
363
// parentComposite.dispose();
364
// }
365
// });
366
}
367
368     /**
369      * Returns the color text.
370      *
371      * @return Color text.
372      */

373     public Label getText() {
374         return text;
375     }
376
377     /**
378      * Sets up the entry fields for configuring the logs. These entry fields are
379      * by default hidden until a log is selected from the table.
380      */

381     private void setupEntryFields() {
382         entryFieldsComposite = new Composite(logConfigComposite, SWT.NONE);
383         GridLayout gridLayout = new GridLayout(5, true);
384         entryFieldsComposite.setLayout(gridLayout);
385         entryFieldsComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
386
387         GridData span4Cells = new GridData(GridData.FILL_HORIZONTAL);
388         span4Cells.horizontalSpan = 4;
389
390         GridData span3Cells = new GridData(GridData.FILL_HORIZONTAL);
391         span3Cells.horizontalSpan = 3;
392
393         Cursor cursorHand = new Cursor(display, SWT.CURSOR_HAND);
394
395         // foreground color
396
setupLeftEntryField(entryFieldsComposite, "Foreground Color");
397
398         foregroundColorText = new Label(entryFieldsComposite, SWT.BORDER | SWT.READ_ONLY);
399         foregroundColorText.setCursor(cursorHand);
400         foregroundColorText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
401
402         foregroundColorText.addMouseListener(new GenericListener() {
403             public void mouseUp(MouseEvent e) {
404                 text = foregroundColorText;
405                 mediator.handleEvent(ActionMediator.EVENT_DISPLAY_COLOR_CHOOSER);
406             }
407         });
408
409         Label pad = new Label(entryFieldsComposite, SWT.NONE);
410         pad.setLayoutData(span3Cells);
411
412         // background color
413
setupLeftEntryField(entryFieldsComposite, "Background Color");
414
415         backgroundColorText = new Label(entryFieldsComposite, SWT.BORDER | SWT.READ_ONLY);
416         backgroundColorText.setCursor(cursorHand);
417         backgroundColorText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
418
419         backgroundColorText.addMouseListener(new GenericListener() {
420             public void mouseUp(MouseEvent e) {
421                 text = backgroundColorText;
422                 mediator.handleEvent(ActionMediator.EVENT_DISPLAY_COLOR_CHOOSER);
423             }
424         });
425
426         pad = new Label(entryFieldsComposite, SWT.NONE);
427         pad.setLayoutData(span3Cells);
428
429         // log name
430
setupLeftEntryField(entryFieldsComposite, "Log Name");
431
432         logNameText = new Text(entryFieldsComposite, SWT.BORDER);
433         logNameText.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
434         logNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
435         logNameText.setTextLimit(MAX_LOG_NAME_LENGTH);
436
437         pad = new Label(entryFieldsComposite, SWT.NONE);
438         pad.setLayoutData(span3Cells);
439
440         // log file path
441
setupLeftEntryField(entryFieldsComposite, "Log File Path");
442
443         logFilePathText = new Text(entryFieldsComposite, SWT.BORDER);
444         logFilePathText.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
445         logFilePathText.setLayoutData(span3Cells);
446
447         Button logFilePathButton = new Button(entryFieldsComposite, SWT.PUSH);
448         logFilePathButton.setText("Browse...");
449         logFilePathButton.setCursor(cursorHand);
450         logFilePathButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
451
452         logFilePathButton.addMouseListener(new GenericListener() {
453             public void mouseUp(MouseEvent e) {
454                 FileDialog fileDialog = new FileDialog(parentShell, SWT.OPEN);
455                 String JavaDoc fileName = fileDialog.open();
456                 if (fileName == null) {
457                     fileName = "";
458                 }
459
460                 logFilePathText.setText(fileName);
461             }
462         });
463
464         hideEntryFields();
465     }
466
467     /**
468      * Displays the instruction label.
469      */

470     private void setupInstruction() {
471         Label label = new Label(logConfigComposite, SWT.WRAP);
472         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
473
474         String JavaDoc msg = "You can attach up to " + MAX_TABLE_ROW
475                 + " server log files here. Please ensure you have sufficient privilege to access these log files.\n\n";
476
477         label.setText(msg);
478     }
479
480     /**
481      * Displays a right aligned label.
482      *
483      * @param composite
484      * Composite parent.
485      * @param value
486      * Label value.
487      */

488     private void setupLeftEntryField(Composite composite, String JavaDoc value) {
489         Label label = new Label(composite, SWT.NONE);
490         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
491         label.setAlignment(SWT.RIGHT);
492         label.setText(value + " : ");
493     }
494
495     /**
496      * Sets up the contents in the table.
497      */

498     private void setupTable() {
499         table = new Table(logConfigComposite, SWT.BORDER | SWT.FULL_SELECTION);
500         table.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
501         table.setLinesVisible(true);
502         table.setHeaderVisible(true);
503         table.setCursor(new Cursor(display, SWT.CURSOR_HAND));
504         table.addListener(SWT.Selection, new GenericListener() {
505             public void handleEvent(Event event) {
506                 TableItem tableItem = (TableItem) event.item;
507                 LogFile logFile = (LogFile) tableItem.getData();
508
509                 selectedLogFileIndex = logFile.getLogOrder();
510                 int foregroundColor = logFile.getForegroundColor();
511                 int backgroundColor = logFile.getBackgroundColor();
512                 String JavaDoc logName = logFile.getLogName();
513                 String JavaDoc logFilePath = logFile.getLogPath();
514
515                 displayEntryFields(foregroundColor, backgroundColor, logName, logFilePath);
516
517             }
518         });
519
520         for (int i = 0; i < TABLE_COLUMN_NAMES.length; ++i) {
521             final TableColumn column = new TableColumn(table, SWT.LEFT);
522             column.setText(TABLE_COLUMN_NAMES[i]);
523             column.setWidth(TABLE_COLUMN_WIDTHS[i]);
524         }
525
526         displayTableContent();
527     }
528 }
529
Popular Tags