View Javadoc

1   /**
2    * Copyright (C) 2007 Joern Krueger surething@users.sourceforge.net
3    * 
4    * This program is free software; you can redistribute
5    * it and/or modify it under the terms of the GNU General
6    * Public License version 2 as published by the Free Software
7    * Foundation.
8    *
9    * This program is distributed in the hope that it will be
10   * useful, but WITHOUT ANY WARRANTY; without even the implied
11   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12   * PURPOSE.  See the GNU General Public License for more
13   * details.
14   *
15   * You should have received a copy of the GNU General Public
16   * License along with this program; if not, write to the
17   * Free Software Foundation, Inc., 59 Temple Place,
18   * Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package de.surething.lda.ui;
22  
23  import java.awt.event.ActionEvent;
24  import java.awt.event.ActionListener;
25  import java.awt.event.MouseAdapter;
26  import java.awt.event.MouseEvent;
27  
28  import javax.swing.JLabel;
29  import javax.swing.JMenuItem;
30  import javax.swing.JPopupMenu;
31  
32  import de.surething.lda.actions.ActionTask;
33  import de.surething.lda.actions.MailActionSettings;
34  import de.surething.lda.locations.Location;
35  import de.surething.lda.locations.LocationSettings;
36  import de.surething.lda.resources.Messages;
37  import de.surethingies.properties.ParameterFactory;
38  import de.surethingies.properties.ui.PropertyEditor;
39  
40  public class MainWindowControl implements ActionListener {
41  
42      private MainWindow mainWindow;
43  
44      private Adapter adapter;
45  
46      public MainWindowControl(MainWindow mainWindow) {
47          this.mainWindow = mainWindow;
48          this.adapter = new Adapter(this);
49      }
50  
51      public Adapter getAdapter() {
52          return adapter;
53      }
54  
55      public void actionPerformed(ActionEvent e) {
56          if (e.getActionCommand().equals(Action.EXIT.toString())) {
57              mainWindow.shutdown();
58  
59          } else if (e.getActionCommand().equals(Action.EXECUTE.toString())) {
60              mainWindow.sleep(true);
61              Thread t = new Thread() {
62                  @Override
63                  public void run() {
64                      actionExecute(mainWindow.getSelectedLocation(), mainWindow.getSelectedAction());
65                      mainWindow.sleep(false);
66                      mainWindow.shutdown();
67                  }
68              };
69              t.start();
70  
71          } else if (e.getActionCommand().equals(Action.ABOUT.toString())) {
72              new AboutDialog(mainWindow, Messages.getString("AboutDialog.Text"));
73  
74          } else {
75              mainWindow.sleep(false);
76              throw new RuntimeException("Unknown Action" + e.getActionCommand());
77          }
78      }
79  
80      private void actionExecute(Location location, ActionTask action) {
81          action.execute(mainWindow, location);
82      }
83  
84      public enum Action {
85          ABOUT(Messages.getString("MainMenu.About")), EXIT(Messages.getString("MainMenu.Exit")), EXECUTE(Messages
86                  .getString("MainMenu.Execute")), CONFIG_LOCATIONS(Messages.getString("MainMenu.ConfigLocations")), CONFIG_ACTIONS(
87                  Messages.getString("MainMenu.ConfigActions"));
88  
89          private String label;
90  
91          private Action(String label) {
92              this.label = label;
93          }
94  
95          public String getLabel() {
96              return label;
97          }
98      }
99  
100     private class Adapter extends MouseAdapter {
101 
102         private JPopupMenu popup;
103 
104         private Adapter(MainWindowControl control) {
105             JMenuItem about = new JMenuItem(Messages.getString("MainMenu.About"));
106             about.setActionCommand(Action.ABOUT.toString());
107             about.addActionListener(control);
108 
109             JMenuItem exit = new JMenuItem(Messages.getString("MainMenu.Exit"));
110             exit.setActionCommand(Action.EXIT.toString());
111             exit.addActionListener(control);
112 
113             popup = new JPopupMenu();
114             popup.add(about);
115             popup.add(exit);
116         }
117 
118         @SuppressWarnings("unchecked")
119         @Override
120         public void mouseClicked(MouseEvent e) {
121             if (e.getButton() == MouseEvent.BUTTON1 && e.getSource() instanceof JLabel) {
122                 String action = ((JLabel) e.getSource()).getName();
123 
124                 if (action.equals(Action.CONFIG_LOCATIONS.toString())) {
125                     Location selectedLocation = (Location) mainWindow.locations.getSelectedItem();
126 
127                     new PropertyEditor(
128                             mainWindow, 
129                             Action.CONFIG_LOCATIONS.getLabel(), 
130                             ParameterFactory.instance().get(
131                                     LocationSettings.GROUP, selectedLocation.getIdentifier()));
132 
133                 } else if (action.equals(Action.CONFIG_ACTIONS.toString())) {
134                     ActionTask selectedAction = (ActionTask) mainWindow.actions.getSelectedItem();
135 
136                     new PropertyEditor(
137                             mainWindow, 
138                             Action.CONFIG_ACTIONS.getLabel(), 
139                             ParameterFactory.instance().get(
140                                     MailActionSettings.GROUP, selectedAction.getIdentifier()));
141 
142                 }
143             }
144         }
145 
146         @Override
147         public void mousePressed(MouseEvent e) {
148             if (e.isPopupTrigger()) {
149                 popup.show(e.getComponent(), e.getX(), e.getY());
150             }
151         }
152     }
153 }