1 package net.sourceforge.tracelog.ui; 2 3 import java.io.File ; 4 import java.util.LinkedList ; 5 import java.util.List ; 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 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 DEFAULT_VALUE = "[available slot]"; 39 40 private static final String [] 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 <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 67 ShellOptionLogConfig() { 68 super(); 69 this.logFiles = new LinkedList <LogFile>(); 70 this.table = null; 71 } 72 73 78 public List <LogFile> getLogFiles() { 79 return logFiles; 80 } 81 82 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 112 public void setLogFiles(List <LogFile> logFiles) { 113 this.logFiles = logFiles; 114 } 115 116 120 private void deleteEntryField() { 121 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 144 private void displayEntryFields(int foregroundColor, int backgroundColor, String logName, String 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 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 logName = DEFAULT_VALUE; 167 String 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 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 204 private void saveConfig() { 205 String msg = ""; 206 207 List <String > existingLogNames = new LinkedList <String >(); 208 209 for (int i = 0; i < logFiles.size(); ++i) { 211 if (i != selectedLogFileIndex) { 212 existingLogNames.add(logFiles.get(i).getLogName()); 213 } 214 } 215 216 int foregroundColor = (Integer ) foregroundColorText.getData(); 218 int backgroundColor = (Integer ) backgroundColorText.getData(); 219 String logName = logNameText.getText(); 220 String logFilePath = logFilePathText.getText(); 221 222 if (Util.isEmpty(logName) || Util.isEmpty(logFilePath)) { 224 msg += "\n- Log name and log file path must all exist."; 225 } 226 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 else if (existingLogNames.contains(logName)) { 232 msg += "\n- Log name already exists. Make sure all log names are unique."; 233 } 234 else if (!new File (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 else if (!new File (logFilePath).isFile()) { 240 msg += "\n- This log file path does not represent a file."; 241 } 242 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 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 289 private void setTableItem(int tableItemIndex, int foregroundColor, int backgroundColor, String logName, String 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 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 } 367 368 373 public Label getText() { 374 return text; 375 } 376 377 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 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 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 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 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 fileName = fileDialog.open(); 456 if (fileName == null) { 457 fileName = ""; 458 } 459 460 logFilePathText.setText(fileName); 461 } 462 }); 463 464 hideEntryFields(); 465 } 466 467 470 private void setupInstruction() { 471 Label label = new Label(logConfigComposite, SWT.WRAP); 472 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 473 474 String 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 488 private void setupLeftEntryField(Composite composite, String 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 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 logName = logFile.getLogName(); 513 String 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 |