001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.addressbook.gui.dialog.group;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Dimension;
022: import java.awt.GridLayout;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.awt.event.KeyEvent;
026: import java.awt.event.KeyListener;
027: import java.util.Map;
028:
029: import javax.swing.BorderFactory;
030: import javax.swing.JButton;
031: import javax.swing.JComponent;
032: import javax.swing.JDialog;
033: import javax.swing.JFrame;
034: import javax.swing.JLabel;
035: import javax.swing.JOptionPane;
036: import javax.swing.JPanel;
037: import javax.swing.JScrollPane;
038: import javax.swing.JTextField;
039: import javax.swing.KeyStroke;
040: import javax.swing.SwingConstants;
041: import javax.swing.text.JTextComponent;
042:
043: import org.columba.addressbook.folder.AbstractFolder;
044: import org.columba.addressbook.gui.autocomplete.AddressCollector;
045: import org.columba.addressbook.gui.autocomplete.DefaultAddressComboBox;
046: import org.columba.addressbook.gui.list.AddressbookDNDListView;
047: import org.columba.addressbook.gui.list.AddressbookListModel;
048: import org.columba.addressbook.model.IContactModelPartial;
049: import org.columba.addressbook.model.IGroupModel;
050: import org.columba.addressbook.util.AddressbookResourceLoader;
051: import org.columba.core.gui.base.ButtonWithMnemonic;
052: import org.columba.core.gui.base.SingleSideEtchedBorder;
053:
054: import com.jgoodies.forms.builder.DefaultFormBuilder;
055: import com.jgoodies.forms.builder.PanelBuilder;
056: import com.jgoodies.forms.layout.CellConstraints;
057: import com.jgoodies.forms.layout.FormLayout;
058:
059: public class EditGroupDialog extends JDialog implements ActionListener,
060: KeyListener {
061: private AddressbookDNDListView list;
062:
063: private JButton addButton;
064:
065: private JButton removeButton;
066:
067: private JLabel nameLabel;
068:
069: private JLabel descriptionLabel;
070:
071: private JTextField nameTextField;
072:
073: private JTextField descriptionTextField;
074:
075: private DefaultAddressComboBox addComboBox;
076:
077: private AddressbookListModel members;
078:
079: private boolean result;
080:
081: private ButtonWithMnemonic okButton;
082:
083: private ButtonWithMnemonic cancelButton;
084:
085: private IGroupModel group;
086:
087: private AbstractFolder parentFolder;
088:
089: /**
090: * Constructor
091: *
092: * @param frame
093: * parent frame
094: * @param groupNode
095: * null, if you want to create a new group. Otherwise, the
096: * groupNode will be modified.
097: */
098: public EditGroupDialog(JFrame frame, IGroupModel group,
099: AbstractFolder parentFolder) {
100: super (frame, true);
101:
102: this .group = group;
103: this .parentFolder = parentFolder;
104:
105: result = false;
106:
107: setTitle(AddressbookResourceLoader.getString("dialog",
108: "editgroupdialog", "contact_list_editor")); //$NON-NLS-1$
109:
110: // set title
111: initComponents();
112:
113: layoutComponents();
114:
115: updateComponents(true);
116:
117: pack();
118:
119: setLocationRelativeTo(null);
120:
121: setVisible(true);
122: }
123:
124: private JPanel createGroupNamePanel() {
125: JPanel panel = new JPanel();
126: FormLayout layout = new FormLayout(
127: "12px, right:default, 6px, default:grow", ""); //$NON-NLS-1$ //$NON-NLS-2$
128:
129: DefaultFormBuilder b = new DefaultFormBuilder(layout, panel);
130: b.setRowGroupingEnabled(true);
131: b.setLeadingColumnOffset(1);
132:
133: b.appendSeparator(AddressbookResourceLoader.getString("dialog",
134: "editgroupdialog", "description_3")); //$NON-NLS-1$
135:
136: b.append(nameLabel);
137: b.append(nameTextField);
138:
139: b.append(descriptionLabel);
140: b.append(descriptionTextField);
141:
142: return panel;
143: }
144:
145: private JPanel createGroupPanel() {
146: JPanel panel = new JPanel();
147: FormLayout layout = new FormLayout(
148: "6dlu, fill:default:grow, 6px, default", //$NON-NLS-1$
149: "default, 12px, default, 6px, default, fill:default:grow"); //$NON-NLS-1$
150:
151: PanelBuilder builder = new PanelBuilder(layout, panel);
152: CellConstraints cc = new CellConstraints();
153:
154: builder
155: .addSeparator(
156: AddressbookResourceLoader.getString("dialog",
157: "editgroupdialog", "group_members"), cc.xywh(1, 1, 4, 1)); //$NON-NLS-1$
158:
159: builder.add(addComboBox, cc.xy(2, 3));
160: builder.add(addButton, cc.xy(4, 3));
161: builder.add(new JScrollPane(list), cc.xywh(2, 5, 1, 2));
162: builder.add(removeButton, cc.xy(4, 5));
163:
164: return panel;
165: }
166:
167: private void layoutComponents() {
168: getContentPane().setLayout(new BorderLayout());
169:
170: JPanel mainPanel = new JPanel(new BorderLayout());
171: mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
172: 12));
173:
174: FormLayout layout = new FormLayout("fill:default:grow", //$NON-NLS-1$
175: "default, 12px, fill:default:grow"); //$NON-NLS-1$
176:
177: CellConstraints cc = new CellConstraints();
178: mainPanel.setLayout(layout);
179:
180: mainPanel.add(createGroupNamePanel(), cc.xy(1, 1));
181: mainPanel.add(createGroupPanel(), cc.xy(1, 3));
182:
183: getContentPane().add(mainPanel, BorderLayout.CENTER);
184:
185: JPanel bottomPanel = new JPanel(new BorderLayout());
186: bottomPanel.setBorder(new SingleSideEtchedBorder(
187: SwingConstants.TOP));
188:
189: JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 6, 0));
190: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
191: 12, 12));
192:
193: buttonPanel.add(okButton);
194: buttonPanel.add(cancelButton);
195: bottomPanel.add(buttonPanel, BorderLayout.EAST);
196:
197: getContentPane().add(bottomPanel, BorderLayout.SOUTH);
198: }
199:
200: private void initComponents() {
201: nameLabel = new JLabel(AddressbookResourceLoader.getString(
202: "dialog", "editgroupdialog", "name")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
203: nameTextField = new JTextField();
204:
205: descriptionLabel = new JLabel(
206: AddressbookResourceLoader.getString(
207: "dialog", "editgroupdialog", "description_2")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
208: descriptionTextField = new JTextField();
209:
210: addComboBox = new DefaultAddressComboBox(parentFolder.getId(),
211: false);
212: ((JTextComponent) addComboBox.getEditor().getEditorComponent())
213: .addKeyListener(this );
214:
215: members = new AddressbookListModel();
216: list = new AddressbookDNDListView(members);
217: list.setMinimumSize(new Dimension(200, 300));
218:
219: addButton = new JButton("Add"); //$NON-NLS-1$
220: addButton.addActionListener(this );
221: addButton.setActionCommand("ADD"); //$NON-NLS-1$
222:
223: removeButton = new JButton("Remove"); //$NON-NLS-1$
224: removeButton.addActionListener(this );
225: removeButton.setActionCommand("REMOVE"); //$NON-NLS-1$
226:
227: okButton = new ButtonWithMnemonic(AddressbookResourceLoader
228: .getString("global", "ok")); //$NON-NLS-1$ //$NON-NLS-2$
229: okButton.setActionCommand("OK"); //$NON-NLS-1$
230: okButton.addActionListener(this );
231:
232: cancelButton = new ButtonWithMnemonic(AddressbookResourceLoader
233: .getString("global", "cancel")); //$NON-NLS-1$ //$NON-NLS-2$
234: cancelButton.setActionCommand("CANCEL"); //$NON-NLS-1$
235: cancelButton.addActionListener(this );
236:
237: getRootPane().setDefaultButton(okButton);
238: getRootPane().registerKeyboardAction(this ,
239: "CANCEL", //$NON-NLS-1$
240: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
241: JComponent.WHEN_IN_FOCUSED_WINDOW);
242: }
243:
244: public boolean getResult() {
245: return result;
246: }
247:
248: public void updateComponents(boolean b) {
249: if (b) {
250: // gettext
251: nameTextField.setText(group.getName()); //$NON-NLS-1$
252: descriptionTextField.setText(group.getDescription()); //$NON-NLS-1$
253:
254: members = new AddressbookListModel();
255:
256: String[] m = group.getMembers();
257:
258: try {
259: Map<String, IContactModelPartial> l = parentFolder
260: .getContactItemMap();
261:
262: for (int i = 0; i < m.length; i++) {
263:
264: IContactModelPartial item = l.get(m[i]);
265:
266: members.addElement(item);
267: }
268: } catch (Exception e) {
269:
270: e.printStackTrace();
271: }
272:
273: this .list.setModel(members);
274: } else {
275: // settext
276: group.setName(nameTextField.getText()); //$NON-NLS-1$
277: group.setDescription(descriptionTextField.getText()); //$NON-NLS-1$
278:
279: // remove all children
280: group.removeAllMembers();
281:
282: // add children
283: for (int i = 0; i < members.getSize(); i++) {
284: IContactModelPartial item = (IContactModelPartial) members
285: .get(i);
286: String uid = item.getId();
287: group.addMember(uid);
288: }
289: }
290: }
291:
292: /**
293: * Add headeritem from ComboBox to List
294: *
295: */
296: private void addHeaderItem() {
297: String s = addComboBox.getText();
298: Object o = AddressCollector.getInstance().getHeaderItem(s);
299:
300: if (o != null) {
301: // this is a headeritem from autocompletion
302: members.addElement((IContactModelPartial) o);
303: addComboBox.setText(""); //$NON-NLS-1$
304: } else {
305: JOptionPane.showMessageDialog(this ,
306: AddressbookResourceLoader.getString("dialog",
307: "editgroupdialog", "You_can_only_add")); //$NON-NLS-1$
308: }
309:
310: // in the future, it will be possible to also add new addresses
311:
312: /*
313: * else { // this is a string // -> check for validity if
314: * (AddressParser.isValid(s)) { HeaderItem item= new
315: * HeaderItem(HeaderItem.CONTACT); item.add("displayname",
316: * AddressParser.getDisplayname(s)); item.add("email;internet",
317: * AddressParser.getAddress(s));
318: *
319: * members.addElement(item); addComboBox.setText(""); } else {
320: * JOptionPane.showMessageDialog( null, s + " is no valid email
321: * address!"); } }
322: */
323: }
324:
325: public void actionPerformed(ActionEvent e) {
326: String command = e.getActionCommand();
327:
328: if (command.equals("CANCEL")) { //$NON-NLS-1$
329: result = false;
330: setVisible(false);
331: } else if (command.equals("OK")) { //$NON-NLS-1$
332:
333: if (nameTextField.getText().length() == 0) {
334: JOptionPane
335: .showMessageDialog(
336: this ,
337: AddressbookResourceLoader
338: .getString(
339: "dialog",
340: "editgroupdialog", "you_must_enter_a_name_for_the_group")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
341:
342: return;
343: }
344:
345: result = true;
346:
347: updateComponents(false);
348:
349: setVisible(false);
350: } else if (command.equals("ADD")) { //$NON-NLS-1$
351: addHeaderItem();
352: } else if (command.equals("REMOVE")) { //$NON-NLS-1$
353:
354: int[] array = list.getSelectedIndices();
355:
356: for (int j = 0; j < array.length; j++) {
357: members.remove(array[j]);
358: }
359: }
360: }
361:
362: /** ************************* KeyListener **************************** */
363: public void keyTyped(KeyEvent e) {
364: }
365:
366: public void keyPressed(KeyEvent e) {
367: }
368:
369: public void keyReleased(KeyEvent e) {
370: char ch = e.getKeyChar();
371:
372: if (ch == KeyEvent.VK_ENTER) {
373: addHeaderItem();
374: }
375: }
376: }
|