KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > editors > ClientsPanel


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package org.terracotta.dso.editors;
6
7 import org.dijon.Button;
8 import org.dijon.ContainerResource;
9 import org.dijon.DictionaryResource;
10 import org.dijon.Label;
11 import org.eclipse.core.resources.IFolder;
12 import org.eclipse.core.resources.IProject;
13 import org.terracotta.dso.TcPlugin;
14 import org.terracotta.dso.editors.chooser.ProjectFolderNavigator;
15 import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureChangeEvent;
16 import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureListener;
17 import org.terracotta.dso.editors.xmlbeans.XmlStringField;
18
19 import com.terracottatech.config.Client;
20 import com.terracottatech.config.TcConfigDocument.TcConfig;
21
22 import java.awt.Frame JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.MouseAdapter JavaDoc;
26 import java.awt.event.MouseEvent JavaDoc;
27
28 public class ClientsPanel extends ConfigurationEditorPanel implements ActionListener JavaDoc, ConfigurationEditorRoot,
29     XmlObjectStructureListener {
30   private static ContainerResource m_res;
31   private IProject m_project;
32   private TcConfig m_config;
33   private Client m_client;
34   private XmlStringField m_logs;
35   private Label m_logsLabel;
36   private Button m_logsButton;
37   private ProjectFolderNavigator m_folderNavigator;
38   private DsoClientDataPanel m_dsoClientData;
39   private ModulesPanel m_modulesPanel;
40
41   static {
42     TcPlugin plugin = TcPlugin.getDefault();
43     DictionaryResource topRes = plugin.getResources();
44
45     m_res = (ContainerResource) topRes.find("ClientsPanel");
46   }
47
48   public ClientsPanel() {
49     super();
50     if (m_res != null) {
51       load(m_res);
52     }
53   }
54
55   public void load(ContainerResource containerRes) {
56     super.load(containerRes);
57
58     m_modulesPanel = (ModulesPanel) findComponent("ModulesPanel");
59
60     m_logs = (XmlStringField) findComponent("Logs");
61     m_logs.init(Client.class, "logs");
62
63     m_logsLabel = (Label) findComponent("LogsLabel");
64     m_logsLabel.addMouseListener(new MouseAdapter JavaDoc() {
65       public void mouseClicked(MouseEvent JavaDoc me) {
66         if (me.getClickCount() == 1) {
67           m_logs.unset();
68         }
69       }
70     });
71
72     m_logsButton = (Button) findComponent("LogsButton");
73     m_logsButton.addActionListener(new ActionListener JavaDoc() {
74       public void actionPerformed(ActionEvent JavaDoc ae) {
75         Frame JavaDoc frame = (Frame JavaDoc) getAncestorOfClass(Frame JavaDoc.class);
76
77         if (m_folderNavigator == null) {
78           m_folderNavigator = new ProjectFolderNavigator(frame);
79         }
80         m_folderNavigator.init(m_project);
81         m_folderNavigator.setActionListener(new LogsNavigatorListener());
82         m_folderNavigator.center(frame);
83         m_folderNavigator.setVisible(true);
84       }
85     });
86
87     m_dsoClientData = (DsoClientDataPanel) findComponent("DsoClientData");
88   }
89
90   class LogsNavigatorListener implements ActionListener JavaDoc {
91     public void actionPerformed(ActionEvent JavaDoc ae) {
92       IFolder folder = m_folderNavigator.getSelectedFolder();
93
94       if (folder != null) {
95         m_logs.setText(folder.getProjectRelativePath().toString());
96         m_logs.set();
97       }
98     }
99   }
100
101   public boolean hasAnySet() {
102     return m_logs.isSet() || m_dsoClientData.hasAnySet();
103   }
104
105   public void ensureXmlObject() {
106     super.ensureXmlObject();
107
108     if (m_client == null) {
109       removeListeners();
110       m_client = m_config.addNewClients();
111       updateChildren();
112       addListeners();
113     }
114   }
115
116   private void syncModel() {
117     if (!hasAnySet() && m_config.getClients() != null) {
118       m_config.unsetClients();
119       m_client = null;
120       fireXmlObjectStructureChanged();
121       updateChildren();
122     } else {
123       setDirty();
124     }
125   }
126
127   private void fireXmlObjectStructureChanged() {
128     fireXmlObjectStructureChanged(m_config);
129   }
130
131   public void actionPerformed(ActionEvent JavaDoc ae) {
132     setDirty();
133   }
134
135   public void structureChanged(XmlObjectStructureChangeEvent e) {
136     syncModel();
137   }
138
139   private void addListeners() {
140     m_logs.addActionListener(this);
141     m_logs.addXmlObjectStructureListener(this);
142
143     m_dsoClientData.addXmlObjectStructureListener(this);
144   }
145
146   private void removeListeners() {
147     m_logs.removeActionListener(this);
148     m_logs.removeXmlObjectStructureListener(this);
149
150     m_dsoClientData.removeXmlObjectStructureListener(this);
151   }
152
153   private void updateChildren() {
154     m_logs.setup(m_client);
155     m_dsoClientData.setup(m_client);
156   }
157
158   public void setup(IProject project) {
159     TcPlugin plugin = TcPlugin.getDefault();
160
161     removeListeners();
162     setEnabled(true);
163
164     m_project = project;
165     m_config = plugin.getConfiguration(project);
166     m_client = m_config != null ? m_config.getClients() : null;
167     m_modulesPanel.setup(m_client);
168     m_modulesPanel.addChangeListener(new ModulesPanel.ModulesEventListener() {
169       public void handleEvent() {
170         fireXmlObjectStructureChanged();
171       }
172     });
173     m_modulesPanel.addSetDirtyListener(new ModulesPanel.ModulesEventListener() {
174       public void handleEvent() {
175         setDirty();
176       }
177     });
178
179     updateChildren();
180     addListeners();
181   }
182
183   public IProject getProject() {
184     return m_project;
185   }
186
187   public void tearDown() {
188     removeListeners();
189
190     m_logs.tearDown();
191     m_dsoClientData.tearDown();
192
193     setEnabled(false);
194   }
195 }
196
Popular Tags