001: /*
002: * ExternalToolsPanel.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.settings;
013:
014: import java.awt.BorderLayout;
015: import java.awt.FontMetrics;
016: import java.beans.PropertyChangeEvent;
017: import java.beans.PropertyChangeListener;
018: import java.util.Enumeration;
019: import java.util.LinkedList;
020: import java.util.List;
021: import javax.swing.DefaultListModel;
022: import javax.swing.JList;
023: import javax.swing.JPanel;
024: import javax.swing.JScrollPane;
025: import javax.swing.ListSelectionModel;
026: import javax.swing.border.EmptyBorder;
027: import javax.swing.event.ListSelectionEvent;
028: import javax.swing.event.ListSelectionListener;
029: import workbench.gui.actions.DeleteListEntryAction;
030: import workbench.gui.actions.NewListEntryAction;
031: import workbench.gui.components.DividerBorder;
032: import workbench.gui.components.WbToolbar;
033: import workbench.interfaces.FileActions;
034: import workbench.interfaces.Restoreable;
035: import workbench.resource.Settings;
036: import workbench.util.ToolDefinition;
037:
038: /**
039: *
040: * @author support@sql-workbench.net
041: */
042: public class ExternalToolsPanel extends JPanel implements Restoreable,
043: ListSelectionListener, FileActions, PropertyChangeListener {
044: private JList toolList;
045: private ToolDefinitionPanel definitionPanel;
046: private WbToolbar toolbar;
047: private DefaultListModel tools;
048:
049: public ExternalToolsPanel() {
050: setLayout(new BorderLayout());
051:
052: toolList = new JList();
053: toolList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
054: toolList.setBorder(new EmptyBorder(2, 1, 2, 1));
055: FontMetrics fm = getFontMetrics(getFont());
056: int width = fm.stringWidth("MMMMMMMMMMMMMMM");
057: toolList.setFixedCellWidth(width);
058: JScrollPane scroll = new JScrollPane(toolList);
059:
060: this .toolbar = new WbToolbar();
061: this .toolbar.add(new NewListEntryAction(this ));
062: this .toolbar.add(new DeleteListEntryAction(this ));
063: toolbar.setBorder(DividerBorder.BOTTOM_DIVIDER);
064:
065: definitionPanel = new ToolDefinitionPanel();
066:
067: add(toolbar, BorderLayout.NORTH);
068: add(scroll, BorderLayout.WEST);
069: add(definitionPanel, BorderLayout.CENTER);
070: }
071:
072: public void saveSettings() {
073: List<ToolDefinition> l = new LinkedList<ToolDefinition>();
074: Enumeration e = this .tools.elements();
075: while (e.hasMoreElements()) {
076: l.add((ToolDefinition) e.nextElement());
077: }
078: Settings.getInstance().setExternalTools(l);
079: }
080:
081: public void restoreSettings() {
082: tools = new DefaultListModel();
083: List<ToolDefinition> t = Settings.getInstance()
084: .getAllExternalTools();
085: for (ToolDefinition tool : t) {
086: tools.addElement(tool);
087: }
088: toolList.setModel(tools);
089: toolList.addListSelectionListener(this );
090: toolList.setSelectedIndex(0);
091: }
092:
093: public void valueChanged(ListSelectionEvent evt) {
094: ToolDefinition def = (ToolDefinition) toolList
095: .getSelectedValue();
096: definitionPanel.setDefinition(def);
097: }
098:
099: public void saveItem() throws Exception {
100: }
101:
102: public void deleteItem() throws Exception {
103: int index = toolList.getSelectedIndex();
104: if (index > -1) {
105: tools.remove(index);
106: }
107: if (toolList.getModel().getSize() == 0) {
108: definitionPanel.setDefinition(null);
109: }
110: toolList.repaint();
111: }
112:
113: public void newItem(boolean copyCurrent) throws Exception {
114: try {
115: ToolDefinition tool = new ToolDefinition("path_to_program",
116: "New Tool");
117: tools.addElement(tool);
118: toolList.setSelectedIndex(tools.size() - 1);
119:
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123: }
124:
125: public void propertyChange(PropertyChangeEvent evt) {
126: if (evt.getPropertyName().equals("name")) {
127: toolList.repaint();
128: }
129: }
130:
131: }
|