001: /*
002: * LnFOptionsPanel.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 javax.swing.AbstractListModel;
019: import javax.swing.JList;
020: import javax.swing.JPanel;
021: import javax.swing.JScrollPane;
022: import javax.swing.ListModel;
023: import javax.swing.ListSelectionModel;
024: import javax.swing.border.EmptyBorder;
025: import javax.swing.event.ListSelectionEvent;
026: import javax.swing.event.ListSelectionListener;
027: import workbench.gui.actions.DeleteListEntryAction;
028: import workbench.gui.actions.NewListEntryAction;
029: import workbench.gui.components.DividerBorder;
030: import workbench.gui.components.WbToolbar;
031: import workbench.gui.lnf.LnFDefinition;
032: import workbench.gui.lnf.LnFManager;
033: import workbench.interfaces.FileActions;
034: import workbench.interfaces.Restoreable;
035: import workbench.log.LogMgr;
036: import workbench.resource.ResourceMgr;
037:
038: /**
039: *
040: * @author support@sql-workbench.net
041: */
042: public class LnFOptionsPanel extends JPanel implements Restoreable,
043: ListSelectionListener, FileActions, PropertyChangeListener {
044: private JList lnfList;
045: private LnFDefinitionPanel definitionPanel;
046: protected LnFManager manager = new LnFManager();
047: private WbToolbar toolbar;
048: private DeleteListEntryAction deleteEntry = null;
049:
050: public LnFOptionsPanel() {
051: setLayout(new BorderLayout());
052:
053: lnfList = new JList();
054: lnfList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
055: lnfList.setBorder(new EmptyBorder(2, 1, 2, 1));
056: FontMetrics fm = getFontMetrics(getFont());
057: int width = fm.stringWidth("MMMMMMMMMMMMMMM");
058: lnfList.setFixedCellWidth(width);
059:
060: JScrollPane scroll = new JScrollPane(lnfList);
061:
062: deleteEntry = new DeleteListEntryAction(this );
063: this .toolbar = new WbToolbar();
064: this .toolbar.add(new NewListEntryAction(this ));
065: this .toolbar.add(deleteEntry);
066: toolbar.setBorder(DividerBorder.BOTTOM_DIVIDER);
067:
068: definitionPanel = new LnFDefinitionPanel();
069: definitionPanel.setPropertyListener(this );
070:
071: add(scroll, BorderLayout.WEST);
072: add(toolbar, BorderLayout.NORTH);
073: add(definitionPanel, java.awt.BorderLayout.CENTER);
074:
075: ListModel model = new LnfList();
076: lnfList.setModel(model);
077: lnfList.addListSelectionListener(this );
078: }
079:
080: public void saveSettings() {
081: manager.saveLookAndFeelDefinitions();
082: }
083:
084: public void restoreSettings() {
085: LnFDefinition clnf = manager.getCurrentLnF();
086: lnfList.setSelectedValue(clnf, true);
087: definitionPanel.setCurrentLookAndFeeld(clnf);
088: }
089:
090: public void valueChanged(ListSelectionEvent evt) {
091: LnFDefinition def = (LnFDefinition) lnfList.getSelectedValue();
092: definitionPanel.setDefinition(def);
093: if (def != null) {
094: this .deleteEntry.setEnabled(!def.isBuiltInLnF());
095: }
096: }
097:
098: public void saveItem() throws Exception {
099: }
100:
101: public void deleteItem() throws Exception {
102: LnFDefinition def = (LnFDefinition) lnfList.getSelectedValue();
103: int index = lnfList.getSelectedIndex();
104: if (def != null) {
105: manager.removeDefinition(def);
106: }
107: if (lnfList.getModel().getSize() == 0) {
108: definitionPanel.setDefinition(null);
109: }
110: if (index >= lnfList.getModel().getSize()) {
111: index = lnfList.getModel().getSize() - 1;
112: }
113: lnfList.setSelectedIndex(index);
114: valueChanged(null);
115: lnfList.repaint();
116: }
117:
118: public void newItem(boolean copyCurrent) throws Exception {
119: try {
120: LnFDefinition def = null;
121: if (copyCurrent) {
122: int index = lnfList.getSelectedIndex();
123: LnFDefinition current = manager
124: .getAvailableLookAndFeels().get(index);
125: def = current.createCopy();
126: } else {
127: String d = ResourceMgr.getString("TxtLnFSample");
128: def = new LnFDefinition(d);
129: }
130: int index = manager.addDefinition(def);
131: lnfList.setSelectedIndex(index);
132: definitionPanel.setDefinition(def);
133: lnfList.updateUI();
134: } catch (Exception e) {
135: LogMgr.logError("LnFOptionsPanel.newItem()",
136: "Error creating new item", e);
137: }
138: }
139:
140: public void propertyChange(PropertyChangeEvent evt) {
141: if (evt.getPropertyName().equals("name")) {
142: lnfList.repaint();
143: }
144: }
145:
146: class LnfList extends AbstractListModel {
147: public Object getElementAt(int index) {
148: return manager.getAvailableLookAndFeels().get(index);
149: }
150:
151: public int getSize() {
152: return manager.getAvailableLookAndFeels().size();
153: }
154: }
155:
156: }
|