KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > addresses > AddressBookView


1 package SnowMailClient.view.addresses;
2
3 import javax.swing.*;
4 import javax.swing.border.*;
5 import javax.swing.event.*;
6 import java.awt.*;
7 import java.awt.event.*;
8 import java.util.*;
9
10 import SnowMailClient.model.*;
11 import snow.sortabletable.*;
12 import snow.utils.gui.*;
13 import SnowMailClient.*;
14 import SnowMailClient.Language.Language;
15
16
17 public class AddressBookView extends JDialog
18 {
19   final private AddressBook addressBook;
20   final private JTable table = new JTable();
21   final private SortableTableModel sortableTableModel;
22   final private JTextField searchTF = new JTextField("",7);
23   final private JTextField searchTF2 = new JTextField("",7);
24   private JTextArea selectedArea = new JTextArea(3,50);
25   final private JButton moveToSpamButton = new JButton(
26           Language.translate("Move to blacklist"),
27           SnowMailClientApp.loadImageIcon("pics/addtospam.PNG"));
28   final private JButton spamListButton = new JButton(
29           Language.translate("View blacklist"));
30
31
32   final private JCheckBox compactViewCB = new JCheckBox(Language.translate("Compact View"), false);
33
34   public int fontSize = UIManager.getFont("Label.font").getSize();
35
36   private static String JavaDoc TITLE = Language.translate("Address Book");
37
38   public AddressBookView(SnowMailClientApp ref)
39   {
40      super(ref, TITLE, true);
41      addressBook = ref.getAddressBook();
42
43
44      setContentPane(new SnowBackgroundPanel(new BorderLayout()));
45
46      sortableTableModel = new SortableTableModel(addressBook);
47      table.setModel(sortableTableModel);
48      sortableTableModel.installGUI(table);
49      table.setOpaque(false);
50      table.getColumnModel().getColumn(2).setMaxWidth(fontSize*9);
51      table.getColumnModel().getColumn(3).setMaxWidth(fontSize*7);
52      table.getColumnModel().getColumn(4).setMaxWidth(fontSize*7);
53      table.getColumnModel().getColumn(5).setMaxWidth(fontSize*7);
54
55
56      // north
57
//
58
JPanel northPanel = new JPanel(new BorderLayout());
59
60      getContentPane().add(northPanel, BorderLayout.NORTH);
61      northPanel.add(
62         new SearchPanel(Language.translate("Search")+": ", null,
63                         sortableTableModel, true, true), BorderLayout.WEST);
64      northPanel.add(compactViewCB, BorderLayout.CENTER);
65      compactViewCB.addActionListener(new ActionListener()
66      {
67        public void actionPerformed(ActionEvent e)
68        {
69          if(compactViewCB.isSelected())
70          {
71            sortableTableModel.setVisibleColumns(new int[]{0,1});
72          }
73          else
74          {
75            int[] cols = new int[sortableTableModel.getBasicTableModel().getColumnCount()];
76            for(int i=0; i<cols.length; i++)
77            {
78              cols[i] = i;
79            }
80            sortableTableModel.setVisibleColumns( cols );
81          }
82        }
83      });
84
85      spamListButton.addActionListener(new ActionListener()
86      {
87        public void actionPerformed(ActionEvent e)
88        {
89          spamListAction();
90          updateTitle();
91        }
92      });
93
94      // center
95
JPanel centerPanel = new JPanel(new BorderLayout());
96      getContentPane().add(centerPanel, BorderLayout.CENTER);
97
98      JScrollPane tableScrollPane = new JScrollPane(table);
99      centerPanel.add(tableScrollPane, BorderLayout.CENTER);
100
101      // south
102
JPanel controlPanel = new JPanel();
103      controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
104      controlPanel.setBorder(new EmptyBorder(5,5,5,5));
105      getContentPane().add(controlPanel, BorderLayout.SOUTH);
106
107      final JButton addBT = new JButton(Language.translate("Add"), Icons.PlusIcon.shared10);
108      GUIUtils.setSmallDimensions(addBT);
109      controlPanel.add(addBT);
110      addBT.addActionListener(new ActionListener()
111      {
112        public void actionPerformed(ActionEvent e)
113        {
114          addAction();
115          updateTitle();
116        }
117      });
118
119      final JButton removeBT = new JButton(Language.translate("Remove"), Icons.CrossIcon.shared10);
120      GUIUtils.setSmallDimensions(removeBT);
121      controlPanel.add(removeBT);
122      removeBT.addActionListener(new ActionListener()
123      {
124        public void actionPerformed(ActionEvent e)
125        {
126          int[] sel = table.getSelectedRows();
127          Vector<Address> addressesToRemove = new Vector<Address>();
128          for(int i=0; i<sel.length; i++)
129          {
130            int pos = sortableTableModel.getIndexInUnsortedFromTablePos(sel[i]);
131            Address address = addressBook.getAddressAt(pos);
132            addressesToRemove.add(address);
133          }
134
135          int rep = JOptionPane.showConfirmDialog(AddressBookView.this,
136             Language.translate("Do you want to delete the % selected addresses ?", ""+addressesToRemove.size()));
137          if(rep!=JOptionPane.OK_OPTION) return;
138
139          for(int i=0; i<addressesToRemove.size(); i++)
140          {
141             addressBook.removeAddress( addressesToRemove.elementAt(i) );
142          }
143          updateTitle();
144        }
145      });
146
147      controlPanel.add(moveToSpamButton);
148      GUIUtils.setSmallDimensions(moveToSpamButton);
149      moveToSpamButton.addActionListener(new ActionListener()
150      {
151        public void actionPerformed(ActionEvent e)
152        {
153          int[] sel = table.getSelectedRows();
154          Vector<Address> addressesToRemove = new Vector<Address>();
155          for(int i=0; i<sel.length; i++)
156          {
157            int pos = sortableTableModel.getIndexInUnsortedFromTablePos(sel[i]);
158            Address address = addressBook.getAddressAt(pos);
159            addressesToRemove.add(address);
160          }
161
162          for(int i=0; i<addressesToRemove.size(); i++)
163          {
164             Address a = addressesToRemove.elementAt(i);
165             SnowMailClientApp.getInstance().getSpamBook().addAddress(a);
166             addressBook.removeAddress( a );
167          }
168
169          updateTitle();
170        }
171      });
172
173      controlPanel.add(Box.createHorizontalStrut(fontSize*5));
174      controlPanel.add(spamListButton);
175      GUIUtils.setSmallDimensions(spamListButton);
176
177      final JButton closeBT = new JButton(Language.translate("Close"), Icons.OkIcon.shared10);
178      GUIUtils.setSmallDimensions(closeBT);
179      controlPanel.add(Box.createHorizontalGlue());
180      controlPanel.add(closeBT);
181      closeBT.setBackground(Color.orange);
182      closeBT.addActionListener(new ActionListener()
183      {
184        public void actionPerformed(ActionEvent e)
185        {
186          terminateFrame();
187          setVisible(false);
188        }
189      });
190
191       table.getSelectionModel().addListSelectionListener(new ListSelectionListener()
192       {
193         public void valueChanged(ListSelectionEvent e)
194         {
195           removeBT.setEnabled(table.getSelectedRowCount()>0);
196           updateSelectedText();
197         }
198       });
199
200
201      addWindowListener(new WindowAdapter()
202      {
203         @Override JavaDoc public void windowClosed(WindowEvent e)
204         {
205            terminateFrame();
206         }
207         // Invoked when a window has been closed.
208
@Override JavaDoc public void windowClosing(WindowEvent e)
209         {
210            terminateFrame();
211         }
212         // Invoked when a window is in the process of being closed.
213
});
214
215      ref.getProperties().setComponentSizeFromINIFile(this,
216         "AddressBookEditor", fontSize*48,fontSize*50,200,100);
217
218      updateTitle();
219
220      setVisible(true); // modal
221

222   } // Constructor
223

224
225
226
227   private void updateTitle()
228   {
229      int total = addressBook.getRowCount();
230      int found = this.sortableTableModel.getRowCount();
231      if(sortableTableModel.isSearchActive())
232      {
233        setTitle(TITLE+" ["+total+" "+Language.translate("addresses")+", "+found+" "+Language.translate("matching search")+"]");
234      }
235      else
236      {
237        setTitle(TITLE+" ["+total+" "+Language.translate("addresses")+"]");
238      }
239   }
240
241   private void updateSelectedText()
242   {
243     this.selectedArea.setText("");
244     int[] sel = table.getSelectedRows();
245     for(int i=0;i<sel.length;i++)
246     {
247       int pos = this.sortableTableModel.getIndexInUnsortedFromTablePos(sel[i]);
248       Address a = this.addressBook.getAddressAt(pos);
249       selectedArea.append(a.getMailAddress());
250       if(i<sel.length-1)
251       {
252         selectedArea.append("; ");
253         if(i%5==4) selectedArea.append("\n");
254       }
255     }
256   }
257
258   private void addAction()
259   {
260      JDialog dialog = new JDialog(this, Language.translate("Add a new address"), true);
261      dialog.setContentPane(new SnowBackgroundPanel(new BorderLayout()));
262      CloseControlPanel ccp = new CloseControlPanel(dialog, true, true, Language.translate("Validate"));
263
264      dialog.getContentPane().add(ccp, BorderLayout.SOUTH);
265
266      JPanel inputPanel = new JPanel();
267      GridLayout3 gl = new GridLayout3(2, inputPanel);
268      dialog.getContentPane().add(inputPanel, BorderLayout.CENTER);
269      JTextField nameTF = new JTextField(25);
270      gl.add(Language.translate("Name")+":");
271      gl.add(nameTF, true);
272      JTextField mailTF = new JTextField(25);
273      gl.add(Language.translate("Mail address")+":");
274      gl.add(mailTF, true);
275
276      dialog.pack();
277      this.centerComponentOnThis(dialog);
278      dialog.setVisible(true);
279      // modal closed
280
if(!ccp.getWasCancelled())
281      {
282         Address a = new Address();
283         a.setName(nameTF.getText());
284         a.setMailAddress(mailTF.getText());
285         addressBook.addAddress(a);
286      }
287
288
289   }
290
291   public void centerComponentOnThis(JDialog comp)
292   {
293     int w = this.getWidth();
294     int h = this.getHeight();
295     int wc = comp.getWidth();
296     int hc = comp.getHeight();
297
298     int x = (int) this.getLocation().getX();
299     int y = (int) this.getLocation().getY();
300
301     comp.setLocation(x+w/2-wc/2,y+h/2-hc/2);
302   }
303
304   private void spamListAction()
305   {
306      new SpamBookView(this);
307   }
308
309
310   private void terminateFrame()
311   {
312      SnowMailClientApp.getInstance().getProperties().saveComponentSizeInINIFile(
313         this, "AddressBookEditor");
314   }
315
316
317
318
319 } // AddressBookView
Popular Tags