001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.mail.gui.config.columns;
018:
019: import java.awt.BorderLayout;
020: import java.awt.Dimension;
021: import java.awt.GridLayout;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.event.KeyEvent;
025:
026: import javax.swing.BorderFactory;
027: import javax.swing.DefaultListSelectionModel;
028: import javax.swing.JButton;
029: import javax.swing.JComponent;
030: import javax.swing.JDialog;
031: import javax.swing.JFrame;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034: import javax.swing.JScrollPane;
035: import javax.swing.KeyStroke;
036: import javax.swing.SwingConstants;
037: import javax.swing.event.ListSelectionEvent;
038: import javax.swing.event.ListSelectionListener;
039:
040: import org.columba.core.gui.base.ButtonWithMnemonic;
041: import org.columba.core.gui.base.SingleSideEtchedBorder;
042: import org.columba.core.gui.util.DialogHeaderPanel;
043: import org.columba.core.help.HelpManager;
044: import org.columba.core.xml.XmlElement;
045: import org.columba.mail.command.IMailFolderCommandReference;
046: import org.columba.mail.folder.IMailbox;
047: import org.columba.mail.folderoptions.ColumnOptionsPlugin;
048: import org.columba.mail.folderoptions.FolderOptionsController;
049: import org.columba.mail.gui.frame.MailFrameMediator;
050: import org.columba.mail.util.MailResourceLoader;
051: import org.frapuccino.checkablelist.CheckableItemImpl;
052: import org.frapuccino.checkablelist.CheckableItemListTableModel;
053: import org.frapuccino.checkablelist.CheckableList;
054:
055: import com.jgoodies.forms.layout.CellConstraints;
056: import com.jgoodies.forms.layout.FormLayout;
057:
058: /**
059: * Configurabe visible columns of the table.
060: * <p>
061: * TODO (@author fdietz): adding of columns is not working currently
062: *
063: * @author fdietz
064: */
065:
066: public class ColumnConfigDialog extends JDialog implements
067: ActionListener, ListSelectionListener {
068:
069: private JButton showButton;
070:
071: private JButton hideButton;
072:
073: private CheckableList list;
074:
075: private int index;
076:
077: private XmlElement columns;
078:
079: private CheckableItemImpl selection;
080:
081: private MailFrameMediator mediator;
082:
083: public ColumnConfigDialog(MailFrameMediator mediator,
084: XmlElement columns) {
085: super ((JFrame) mediator.getView().getFrame(),
086: MailResourceLoader.getString("dialog", "columns",
087: "title"), true);
088: this .mediator = mediator;
089: this .columns = columns;
090:
091: list = new CheckableList();
092: list.getSelectionModel().addListSelectionListener(this );
093:
094: initComponents();
095: updateComponents(true);
096:
097: getRootPane().registerKeyboardAction(this , "CLOSE",
098: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
099: JComponent.WHEN_IN_FOCUSED_WINDOW);
100: pack();
101: setLocationRelativeTo(null);
102: setVisible(true);
103: }
104:
105: protected JPanel createButtonPanel() {
106: JPanel bottom = new JPanel();
107: bottom.setLayout(new BorderLayout());
108:
109: bottom.setBorder(BorderFactory
110: .createEmptyBorder(12, 12, 12, 12));
111:
112: ButtonWithMnemonic cancelButton = new ButtonWithMnemonic(
113: MailResourceLoader.getString("global", "cancel"));
114:
115: //$NON-NLS-1$ //$NON-NLS-2$
116: cancelButton.addActionListener(this );
117: cancelButton.setActionCommand("CANCEL"); //$NON-NLS-1$
118:
119: ButtonWithMnemonic okButton = new ButtonWithMnemonic(
120: MailResourceLoader.getString("global", "ok"));
121:
122: //$NON-NLS-1$ //$NON-NLS-2$
123: okButton.addActionListener(this );
124: okButton.setActionCommand("OK"); //$NON-NLS-1$
125: okButton.setDefaultCapable(true);
126: getRootPane().setDefaultButton(okButton);
127:
128: ButtonWithMnemonic helpButton = new ButtonWithMnemonic(
129: MailResourceLoader.getString("global", "help"));
130:
131: // associate with JavaHelp
132: HelpManager.getInstance().enableHelpOnButton(helpButton,
133: "configuring_columba");
134: HelpManager.getInstance().enableHelpKey(getRootPane(),
135: "configuring_columba");
136:
137: JPanel buttonPanel = new JPanel();
138: buttonPanel.setLayout(new GridLayout(1, 3, 6, 0));
139: buttonPanel.add(okButton);
140: buttonPanel.add(cancelButton);
141: buttonPanel.add(helpButton);
142:
143: bottom.add(buttonPanel, BorderLayout.EAST);
144: return bottom;
145: }
146:
147: public void initComponents() {
148:
149: showButton = new ButtonWithMnemonic(MailResourceLoader
150: .getString("dialog", "columns", "show"));
151: showButton.setActionCommand("SHOW");
152: showButton.addActionListener(this );
153: showButton.setEnabled(false);
154:
155: hideButton = new ButtonWithMnemonic(MailResourceLoader
156: .getString("dialog", "columns", "hide"));
157: hideButton.setActionCommand("HIDE");
158: hideButton.setEnabled(false);
159: hideButton.addActionListener(this );
160:
161: getContentPane().add(createPanel(), BorderLayout.CENTER);
162:
163: getContentPane().add(createBottomPanel(), BorderLayout.SOUTH);
164:
165: getContentPane().add(
166: new DialogHeaderPanel(MailResourceLoader.getString(
167: "dialog", "columns", "header_title"),
168: MailResourceLoader.getString("dialog",
169: "columns", "header_description")),
170: BorderLayout.NORTH);
171: }
172:
173: private JPanel createPanel() {
174: JPanel jpanel1 = new JPanel();
175: FormLayout formlayout1 = new FormLayout(
176: "FILL:DEFAULT:GROW(1.0),3DLU,FILL:DEFAULT:NONE",
177: "CENTER:DEFAULT:NONE,1DLU,FILL:DEFAULT:GROW(1.0),3DLU,CENTER:DEFAULT:NONE");
178: CellConstraints cc = new CellConstraints();
179: jpanel1.setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
180: 12));
181: jpanel1.setLayout(formlayout1);
182:
183: JLabel jlabel1 = new JLabel();
184: jlabel1.setText("Columns:");
185: jpanel1.add(jlabel1, cc.xy(1, 1));
186:
187: JScrollPane scrollPane = new JScrollPane(list);
188: scrollPane.setPreferredSize(new Dimension(250, 200));
189: jpanel1.add(scrollPane, cc.xy(1, 3));
190:
191: jpanel1.add(createPanel1(), new CellConstraints(3, 3, 1, 1,
192: CellConstraints.DEFAULT, CellConstraints.TOP));
193:
194: return jpanel1;
195: }
196:
197: private JPanel createPanel1() {
198: JPanel jpanel1 = new JPanel();
199: FormLayout formlayout1 = new FormLayout("FILL:DEFAULT:NONE",
200: "CENTER:DEFAULT:NONE,3DLU,CENTER:DEFAULT:NONE,3DLU,CENTER:DEFAULT:NONE");
201: CellConstraints cc = new CellConstraints();
202: jpanel1.setLayout(formlayout1);
203:
204: jpanel1.add(showButton, cc.xy(1, 1));
205:
206: jpanel1.add(hideButton, cc.xy(1, 3));
207:
208: return jpanel1;
209: }
210:
211: /**
212: *
213: */
214: private JPanel createBottomPanel() {
215: JPanel bottomPanel = new JPanel(new BorderLayout());
216: bottomPanel.setBorder(new SingleSideEtchedBorder(
217: SwingConstants.TOP));
218:
219: JPanel buttonPanel = createButtonPanel();
220:
221: bottomPanel.add(buttonPanel, BorderLayout.EAST);
222:
223: return bottomPanel;
224: }
225:
226: private XmlElement findColumn(XmlElement parent, String name) {
227: for (int i = 0; i < parent.count(); i++) {
228: XmlElement child = parent.getElement(i);
229: if (child.getAttribute("name").equals(name)) {
230: return child;
231: }
232: }
233: return null;
234: }
235:
236: public void updateComponents(boolean b) {
237: if (b) {
238: CheckableItemListTableModel model = new CheckableItemListTableModel();
239: String[] stringList = ColumnOptionsPlugin.getColumns();
240:
241: for (int j = 0; j < stringList.length; j++) {
242: String c = stringList[j];
243: CheckableItemImpl item = new CheckableItemImpl(c);
244: XmlElement element = findColumn(columns, c);
245: item.setSelected(element != null);
246: model.addElement(item);
247: }
248:
249: list.setModel(model);
250: } else {
251: CheckableItemListTableModel model = ((CheckableItemListTableModel) list
252: .getModel());
253:
254: for (int i = 0; i < model.count(); i++) {
255: // get column of list
256: CheckableItemImpl column = (CheckableItemImpl) model
257: .getElement(i);
258:
259: // find colum
260: XmlElement element = findColumn(columns, column
261: .toString());
262:
263: if (element != null) {
264: // remove disabled column
265: if (!column.isSelected()) {
266: element.removeFromParent();
267: }
268: } else {
269: if (column.isSelected()) {
270: XmlElement newElement = new XmlElement("column");
271: newElement.addAttribute("name", column
272: .toString());
273: newElement.addAttribute("width", "100");
274: columns.addElement(newElement);
275: }
276: }
277: }
278: }
279: }
280:
281: public void valueChanged(ListSelectionEvent e) {
282: if (e.getValueIsAdjusting()) {
283: return;
284: }
285:
286: DefaultListSelectionModel theList = (DefaultListSelectionModel) e
287: .getSource();
288: if (!theList.isSelectionEmpty()) {
289: index = theList.getAnchorSelectionIndex();
290:
291: selection = (CheckableItemImpl) ((CheckableItemListTableModel) list
292: .getModel()).getElement(index);
293:
294: updateButtonState();
295: }
296: }
297:
298: private void updateButtonState() {
299: if (selection.isSelected()) {
300: hideButton.setEnabled(true);
301: showButton.setEnabled(false);
302: } else {
303: showButton.setEnabled(true);
304: hideButton.setEnabled(false);
305: }
306: }
307:
308: public void actionPerformed(ActionEvent e) {
309: String action = e.getActionCommand();
310:
311: if (action.equals("OK")) {
312: updateComponents(false);
313:
314: setVisible(false);
315:
316: ColumnOptionsPlugin plugin = (ColumnOptionsPlugin) ((FolderOptionsController) mediator
317: .getFolderOptionsController())
318: .getPlugin("ColumnOptions");
319:
320: // make sure this configuration is also visually working immediately
321: IMailFolderCommandReference r = mediator.getTreeSelection();
322: plugin.loadOptionsFromXml((IMailbox) r.getSourceFolder());
323: } else if (action.equals("CANCEL")) {
324: setVisible(false);
325: } else if (action.equals("SHOW")) {
326: if (selection != null) {
327: selection.setSelected(!selection.isSelected());
328: ((CheckableItemListTableModel) list.getModel())
329: .updateRow(selection);
330:
331: // list.repaint();
332: updateButtonState();
333: }
334: } else if (action.equals("HIDE")) {
335: // disable selected item
336: if (selection != null) {
337: selection.setSelected(!selection.isSelected());
338: ((CheckableItemListTableModel) list.getModel())
339: .updateRow(selection);
340:
341: // list.repaint();
342: updateButtonState();
343: }
344: }
345: }
346: }
|