1 21 package org.columba.core.gui.scripting; 22 23 import java.awt.BorderLayout ; 24 import java.awt.Color ; 25 import java.awt.Dimension ; 26 import java.awt.FlowLayout ; 27 import java.awt.Frame ; 28 import java.awt.GridLayout ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.util.logging.Logger ; 32 33 import javax.swing.BorderFactory ; 34 import javax.swing.JButton ; 35 import javax.swing.JDialog ; 36 import javax.swing.JOptionPane ; 37 import javax.swing.JPanel ; 38 import javax.swing.JScrollPane ; 39 import javax.swing.JTabbedPane ; 40 import javax.swing.JTable ; 41 import javax.swing.ListSelectionModel ; 42 import javax.swing.SwingConstants ; 43 import javax.swing.SwingUtilities ; 44 import javax.swing.event.ListSelectionEvent ; 45 import javax.swing.event.ListSelectionListener ; 46 import javax.swing.table.DefaultTableCellRenderer ; 47 import javax.swing.table.TableColumnModel ; 48 import javax.swing.table.TableModel ; 49 50 import org.columba.core.gui.base.ButtonWithMnemonic; 51 import org.columba.core.gui.base.SingleSideEtchedBorder; 52 import org.columba.core.scripting.ScriptLogger; 53 import org.columba.core.scripting.model.ColumbaScript; 54 55 58 public class ScriptManager 59 extends JDialog 60 implements ActionListener 61 { 62 63 private static final Logger LOG = Logger.getLogger(ScriptManager.class.getName()); 64 65 66 67 private static final String 68 RES_WINDOW_TITLE = "Macros", 69 RES_EDIT_BUTTON = "&Edit", 70 RES_REFRESH_BUTTON = "&Refresh", 71 RES_REMOVE_BUTTON = "Re&move", 72 RES_CLOSE_BUTTON = "&Close", 73 RES_LOG_DETAILS_BUTTON = "&Details...", 74 RES_CLEAR_LOG_BUTTON = "C&lear", 75 RES_TAB_SCRIPTS = "Scripts", 76 RES_TAB_LOG = "Log", 77 RES_REMOVE_MESSAGE = "The selected scripts will be removed from disk.\nContinue?", 78 RES_WARNING_DIALOG_TITLE = "Remove scripts"; 79 80 private static final String 81 EDIT_SCRIPT_ACTION = "edit", 82 REMOVE_SCRIPT_ACTION = "remove", 83 REFRESH_LIST_ACTION = "refresh", 84 CLOSE_ACTION = "close", 85 LOG_DETAILS_ACTION = "log_details", 86 CLEAR_LOG_ACTION = "clear_log"; 87 88 private JTable 89 scriptsList, 90 logList; 91 92 private JButton 93 editScriptButton, 94 removeScriptButton, 95 refreshListButton, 96 logDetailsButton, 97 clearLogButton; 98 99 100 private ScriptManagerDocument document; 101 102 public ScriptManager(Frame parent, ScriptManagerDocument doc) 103 { 104 super(parent, RES_WINDOW_TITLE, true); 105 document = doc; 106 107 initGui(); 108 setLocationRelativeTo(getParent()); 109 110 } 111 112 private void initGui() 113 { 114 115 JPanel bottomPanel = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 116 bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP)); 117 bottomPanel.add(createDialogActionsPanel()); 118 119 getContentPane().setLayout(new BorderLayout ()); 120 JTabbedPane tabs = new JTabbedPane (); 121 tabs.add(RES_TAB_SCRIPTS, createScriptsPanel()); 122 tabs.add(RES_TAB_LOG, createLogPanel()); 123 getContentPane().add(tabs, BorderLayout.CENTER); 124 getContentPane().add(bottomPanel, BorderLayout.SOUTH); 125 126 setButtonStatus(); 127 128 pack(); 129 130 } 131 132 private JPanel createLogPanel() 133 { 134 JPanel panel = new JPanel (new BorderLayout (10, 0)); 135 panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 136 panel.add(createLogList(), BorderLayout.CENTER); 137 panel.add(createLogListActionsPanel(), BorderLayout.EAST); 138 return panel; 139 } 140 141 private JPanel createLogListActionsPanel() 142 { 143 JPanel panel = new JPanel (new GridLayout (2, 1, 0, 5)); 144 145 logDetailsButton = new ButtonWithMnemonic(RES_LOG_DETAILS_BUTTON); 146 clearLogButton = new ButtonWithMnemonic(RES_CLEAR_LOG_BUTTON); 147 148 logDetailsButton.setActionCommand(LOG_DETAILS_ACTION); 149 clearLogButton.setActionCommand(CLEAR_LOG_ACTION); 150 151 logDetailsButton.addActionListener(this); 152 clearLogButton.addActionListener(this); 153 154 panel.add(logDetailsButton); 155 panel.add(clearLogButton); 156 157 JPanel container = new JPanel (new BorderLayout ()); 158 container.add(panel, BorderLayout.NORTH); 159 return container; 160 161 } 162 163 164 private JPanel createLogList() 165 { 166 JPanel panel = new JPanel (new BorderLayout ()); 167 168 logList = new JTable (new ScriptLogTableModel()); 169 170 logList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 171 logList.setShowGrid(true); 172 logList.sizeColumnsToFit(JTable.AUTO_RESIZE_ALL_COLUMNS); 173 174 logList.getSelectionModel() 175 .addListSelectionListener(new ListSelectionListener () 176 { 177 public void valueChanged(ListSelectionEvent e) 178 { 179 if (e.getValueIsAdjusting()) return; 180 181 setButtonStatus(); 182 } 183 184 }); 185 186 TableColumnModel tcm = logList.getColumnModel(); 187 tcm.getColumn(ScriptLogTableModel.MESSAGE_COLUMN).setPreferredWidth(400); 188 tcm.getColumn(ScriptLogTableModel.MESSAGE_COLUMN) 189 .setCellRenderer(new DefaultTableCellRenderer () 190 { 191 protected void setValue(Object value) 192 { 193 setText(((ScriptLogger.LogEntry) value).getMessage()); 194 } 195 }); 196 197 JScrollPane scrollPane = new JScrollPane (logList); 198 scrollPane.setPreferredSize(new Dimension (450, 200)); 199 scrollPane.getViewport().setBackground(Color.white); 200 panel.add(scrollPane, BorderLayout.CENTER); 201 202 return panel; 203 } 204 205 private JPanel createScriptsPanel() 206 { 207 208 JPanel panel = new JPanel (new BorderLayout (10, 0)); 209 panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 210 panel.add(createScriptList(), BorderLayout.CENTER); 211 panel.add(createScriptActionsPanel(), BorderLayout.EAST); 212 213 return panel; 214 215 } 216 217 218 private JPanel createScriptList() 219 { 220 JPanel panel = new JPanel (new BorderLayout ()); 221 222 scriptsList = new JTable (new ScriptsTableModel(document)); 223 224 scriptsList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 225 scriptsList.setShowGrid(false); 226 scriptsList.sizeColumnsToFit(JTable.AUTO_RESIZE_NEXT_COLUMN); 227 228 scriptsList.getSelectionModel() 229 .addListSelectionListener(new ListSelectionListener () 230 { 231 public void valueChanged(ListSelectionEvent e) 232 { 233 if (e.getValueIsAdjusting()) return; 234 235 setButtonStatus(); 236 } 237 }); 238 239 TableColumnModel tcm = scriptsList.getColumnModel(); 240 tcm.getColumn(ScriptsTableModel.NAME_COLUMN).setPreferredWidth(100); 241 tcm.getColumn(ScriptsTableModel.NAME_COLUMN) 242 .setCellRenderer(new DefaultTableCellRenderer () 243 { 244 protected void setValue(Object value) 245 { 246 setText(((ColumbaScript) value).getName()); 247 } 248 }); 249 250 tcm.getColumn(ScriptsTableModel.AUTHOR_COLUMN).setPreferredWidth(100); 251 tcm.getColumn(ScriptsTableModel.DESCRIPTION_COLUMN).setPreferredWidth(250); 252 253 JScrollPane scrollPane = new JScrollPane (scriptsList); 254 scrollPane.setPreferredSize(new Dimension (450, 200)); 255 scrollPane.getViewport().setBackground(Color.white); 256 panel.add(scrollPane, BorderLayout.CENTER); 257 258 return panel; 259 } 260 261 private JPanel createScriptActionsPanel() 262 { 263 264 JPanel scriptActionsPanel = new JPanel (new GridLayout (3, 1, 0, 5)); 265 266 editScriptButton = new ButtonWithMnemonic(RES_EDIT_BUTTON); 267 removeScriptButton = new ButtonWithMnemonic(RES_REMOVE_BUTTON); 268 refreshListButton = new ButtonWithMnemonic(RES_REFRESH_BUTTON); 269 270 editScriptButton.setActionCommand(EDIT_SCRIPT_ACTION); 271 removeScriptButton.setActionCommand(REMOVE_SCRIPT_ACTION); 272 refreshListButton.setActionCommand(REFRESH_LIST_ACTION); 273 274 editScriptButton.addActionListener(this); 275 removeScriptButton.addActionListener(this); 276 refreshListButton.addActionListener(this); 277 278 scriptActionsPanel.add(editScriptButton); 279 scriptActionsPanel.add(removeScriptButton); 280 scriptActionsPanel.add(refreshListButton); 281 282 JPanel container = new JPanel (new BorderLayout ()); 283 container.add(scriptActionsPanel, BorderLayout.NORTH); 284 285 return container; 286 } 287 288 private void setButtonStatus() 289 { 290 SwingUtilities.invokeLater(new Runnable () 291 { 292 public void run() 293 { 294 295 boolean 296 scriptSelected = scriptsList.getSelectedRowCount() > 0, 297 hasLogMessages = logList.getRowCount() > 0, 298 logMessageSelected = logList.getSelectedRow() > -1; 299 300 editScriptButton.setEnabled(scriptSelected); 301 removeScriptButton.setEnabled(scriptSelected); 302 refreshListButton.setEnabled(scriptSelected); 303 304 if (logMessageSelected) 305 { 306 ScriptLogger.LogEntry entry = 307 (ScriptLogger.LogEntry)logList.getModel().getValueAt(logList.getSelectedRow(), 308 ScriptLogTableModel.MESSAGE_COLUMN); 309 310 logDetailsButton.setEnabled(entry.getDetails() != null && 311 entry.getDetails().length() > 0); 312 } 313 else 314 { 315 logDetailsButton.setEnabled(false); 316 } 317 318 clearLogButton.setEnabled(hasLogMessages); 319 320 } 321 }); 322 323 324 } 325 326 private JPanel createDialogActionsPanel() 327 { 328 329 JButton closeButton = new ButtonWithMnemonic(RES_CLOSE_BUTTON); 330 JPanel actionsPanel = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 331 332 closeButton.setActionCommand(CLOSE_ACTION); 333 closeButton.addActionListener(this); 334 actionsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 335 actionsPanel.add(closeButton); 336 337 return actionsPanel; 338 339 } 340 341 public void actionPerformed(ActionEvent event) 342 { 343 String command = event.getActionCommand(); 344 345 if (command.equals(CLOSE_ACTION)) 346 { 347 close(); 348 } 349 else if (command.equals(REMOVE_SCRIPT_ACTION)) 350 { 351 352 if (JOptionPane.showConfirmDialog(this, 353 RES_REMOVE_MESSAGE, 354 RES_WARNING_DIALOG_TITLE, 355 JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) 356 { 357 return; 358 } 359 360 int[] selectedIndexes = scriptsList.getSelectedRows(); 361 TableModel model = scriptsList.getModel(); 362 363 ColumbaScript[] scripts = new ColumbaScript[selectedIndexes.length]; 364 for (int i = 0; i < selectedIndexes.length; i++) 365 { 366 scripts[i] = (ColumbaScript) model.getValueAt( selectedIndexes[i], 367 ScriptsTableModel.NAME_COLUMN); 368 } 369 document.removeScript(scripts); 370 } 371 else if (command.equals(REFRESH_LIST_ACTION)) 372 { 373 document.refreshScriptList(); 374 } 375 else if (command.equals(EDIT_SCRIPT_ACTION)) 376 { 377 JOptionPane.showMessageDialog(this, 378 "Ha ha, wouldn't it be really sweet if this were implemented?", 379 "TODO", 380 JOptionPane.INFORMATION_MESSAGE); 381 } 382 else if (command.equals(LOG_DETAILS_ACTION)) 383 { 384 ScriptLogger.LogEntry entry = 385 (ScriptLogger.LogEntry)logList.getModel().getValueAt(logList.getSelectedRow(), 386 ScriptLogTableModel.MESSAGE_COLUMN); 387 388 new MessageDetailsDialog(this,entry).setVisible(true); 389 390 } 391 else if (command.equals(CLEAR_LOG_ACTION)) 392 { 393 ((ScriptLogTableModel) logList.getModel()).clearLog(); 394 } 395 else 396 { 397 LOG.warning("Not handling: " + event.getActionCommand()); 398 } 399 400 setButtonStatus(); 401 } 402 403 private void close() 404 { 405 406 ((ScriptLogTableModel) logList.getModel()).dispose(); 407 408 setVisible(false); 409 dispose(); 410 } 411 412 } 413 | Popular Tags |