BrightSide Workbench Full Report + Source Code
WorkingDesktop.java
Go to the documentation of this file.
1 /*
2  * Turró i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis Turró Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.wd.desktop;
19 
20 import java.net.URLDecoder;
21 import java.text.ParseException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.apache.pivot.collections.Map;
25 import org.apache.pivot.util.Resources;
26 import org.apache.pivot.util.concurrent.Task;
27 import org.apache.pivot.util.concurrent.TaskExecutionException;
28 import org.apache.pivot.util.concurrent.TaskListener;
29 import org.apache.pivot.wtk.Application;
30 import org.apache.pivot.wtk.Button;
31 import org.apache.pivot.wtk.ButtonGroup;
32 import org.apache.pivot.wtk.ButtonPressListener;
33 import org.apache.pivot.wtk.DesktopApplicationContext;
34 import org.apache.pivot.wtk.Dialog;
35 import org.apache.pivot.wtk.Display;
36 import org.apache.pivot.wtk.PushButton;
37 import org.apache.pivot.wtk.TaskAdapter;
38 import org.apache.pivot.wtk.TextInput;
39 import org.apache.pivot.wtk.TreeView;
40 import org.apache.pivot.wtk.Window;
41 import org.apache.pivot.wtk.content.TreeNode;
42 import org.apache.pivot.wtkx.WTKX;
43 import org.apache.pivot.wtkx.WTKXSerializer;
44 import org.turro.pivot.dialog.ProgressSheet;
45 import org.turro.wd.entities.WdRuntime;
46 import org.turro.wd.files.WorkingDirectory;
47 import org.turro.wd.tree.DelTask;
48 import org.turro.wd.tree.RefreshTask;
49 import org.turro.wd.tree.SyncTask;
50 import org.turro.wd.tree.TreeDirectory;
51 import org.turro.wd.tree.TreeUtil;
52 import org.turro.wd.tree.TreeWorkFile;
53 import org.turro.wd.tree.WorkingTreeModel;
54 
55 public class WorkingDesktop implements Application {
56 
57  private Window window = null;
58  private Dialog commit = null;
59  @WTKX private TreeView workingTreeView;
60  @WTKX private PushButton syncButton, openButton, refreshButton,
61  deleteButton, doSyncButton;
62  @WTKX private TextInput commentUpdateInput, commentCommitInput;
63  @WTKX private ButtonGroup updateAction, commitAction;
64  private WorkingDirectory workingDirectory;
65 
66  @Override
67  public void startup(Display display, Map<String, String> properties)
68  throws Exception {
69 
70  final String bbUser = properties.get("bbUser");
71  final String bbHost = properties.get("bbHost");
72  final String bbPort = properties.get("bbPort");
73  final String bbContext = properties.get("bbContext");
74  final String bbCreateDir = URLDecoder.decode(properties.get("bbCreateDir"), "UTF-8");
75  final String bbCreatePath = URLDecoder.decode(properties.get("bbCreatePath"), "UTF-8");
76 
77 // final String bbUser = "llturro";
78 // final String bbHost = "localhost";
79 // final String bbPort = "8084";
80 // final String bbContext = "/portal30";
81 // final String bbCreateDir = "Lluis Turró Cutiller";
82 // final String bbCreatePath = "/contact/5525d41a1b548b47011b548c98b00001";
83 
84  final Resources resources = new Resources(getClass().getName());
85 
86  WTKXSerializer wtkxSerializer = new WTKXSerializer(resources);
87  window = (Window) wtkxSerializer.readObject(this, "working_desktop.wtkx");
88  wtkxSerializer.bind(this, WorkingDesktop.class);
89 
90  commit = (Dialog) wtkxSerializer.readObject(this, "commit_dialog.wtkx");
91  wtkxSerializer.bind(this, WorkingDesktop.class);
92 
93  ProgressSheet.getInstance(window);
94 
95  syncButton.getButtonPressListeners().add(new ButtonPressListener() {
96  @Override
97  public void buttonPressed(Button button) {
98  commit.setModal(true);
99  commit.open(window);
100  }
101  });
102 
103  doSyncButton.getButtonPressListeners().add(new ButtonPressListener() {
104  public void buttonPressed(Button button) {
105  commit.close();
106 
107  SyncTask syncTask = new SyncTask(workingTreeView, resources.getString("synchronize"),
108  commentCommitInput.getText(), getSelection(commitAction),
109  commentUpdateInput.getText(), getSelection(updateAction));
110  TaskListener<String> taskListener = new TaskListener<String>() {
111 
112  @Override
113  public void taskExecuted(Task<String> task) {
114  try {
115  workingDirectory.reload();
116  workingTreeView.setTreeData(new WorkingTreeModel(workingDirectory));
117  TreeUtil.expandRootBranches(workingTreeView);
118  System.out.println("Synchronous task execution complete: \"" + task.getResult() + "\"");
119  } catch (ParseException ex) {
120  Logger.getLogger(WorkingDesktop.class.getName()).log(Level.SEVERE, null, ex);
121  }
122  }
123 
124  @Override
125  public void executeFailed(Task<String> task) {
126  try {
127  workingDirectory.reload();
128  workingTreeView.setTreeData(new WorkingTreeModel(workingDirectory));
129  TreeUtil.expandRootBranches(workingTreeView);
130  System.err.println(task.getFault());
131  } catch (ParseException ex) {
132  Logger.getLogger(WorkingDesktop.class.getName()).log(Level.SEVERE, null, ex);
133  }
134  }
135  };
136 
137  syncTask.execute(new TaskAdapter<String>(taskListener));
138  }
139  });
140 
141  openButton.getButtonPressListeners().add(new ButtonPressListener() {
142  @Override
143  public void buttonPressed(Button button) {
144  TreeNode tn = (TreeNode) workingTreeView.getSelectedNode();
145  if(tn instanceof TreeDirectory) {
146  WdRuntime.launch(((TreeDirectory) tn).getDirectory().getSystemFile().getAbsolutePath());
147  } else if(tn instanceof TreeWorkFile) {
148  WdRuntime.launch(((TreeWorkFile) tn).getWorkFile().getSystemFile().getAbsolutePath());
149  }
150  }
151  });
152 
153  refreshButton.getButtonPressListeners().add(new ButtonPressListener() {
154  @Override
155  public void buttonPressed(Button button) {
156  new RefreshTask() {
157 
158  @Override
159  public Object execute() throws TaskExecutionException {
160  try {
161  workingDirectory.reload();
162  workingTreeView.setTreeData(new WorkingTreeModel(workingDirectory));
163  TreeUtil.expandRootBranches(workingTreeView);
164  } catch (Exception ex) {
165  Logger.getLogger(WorkingDesktop.class.getName()).log(Level.SEVERE, null, ex);
166 // } catch (ParseException ex) {
167 // Logger.getLogger(WorkingDesktop.class.getName()).log(Level.SEVERE, null, ex);
168  }
169  return null;
170  }
171  }.refreshData();
172  }
173  });
174 
175  deleteButton.getButtonPressListeners().add(new ButtonPressListener() {
176  @Override
177  public void buttonPressed(Button button) {
178  DelTask delTask = new DelTask(workingTreeView, resources.getString("deleteLocally"));
179  TaskListener<String> taskListener = new TaskListener<String>() {
180 
181  @Override
182  public void taskExecuted(Task<String> task) {
183  try {
184  workingDirectory.reload();
185  workingTreeView.setTreeData(new WorkingTreeModel(workingDirectory));
186  TreeUtil.expandRootBranches(workingTreeView);
187  System.out.println("Deletion task execution complete: \"" + task.getResult() + "\"");
188  } catch (ParseException ex) {
189  Logger.getLogger(WorkingDesktop.class.getName()).log(Level.SEVERE, null, ex);
190  }
191  }
192 
193  @Override
194  public void executeFailed(Task<String> task) {
195  try {
196  workingDirectory.reload();
197  workingTreeView.setTreeData(new WorkingTreeModel(workingDirectory));
198  TreeUtil.expandRootBranches(workingTreeView);
199  System.err.println(task.getFault());
200  } catch (ParseException ex) {
201  Logger.getLogger(WorkingDesktop.class.getName()).log(Level.SEVERE, null, ex);
202  }
203  }
204  };
205 
206  delTask.execute(new TaskAdapter<String>(taskListener));
207  }
208  });
209 
210  window.open(display);
211 
212  new RefreshTask() {
213 
214  @Override
215  public Object execute() throws TaskExecutionException {
216  try {
217  workingDirectory = new WorkingDirectory(bbUser, bbHost, bbPort, bbContext);
218  workingDirectory.initialize(bbCreateDir, bbCreatePath);
219  workingTreeView.setCheckmarksEnabled(true);
220  workingTreeView.setSelectMode(TreeView.SelectMode.SINGLE);
221  workingTreeView.setShowMixedCheckmarkState(true);
222  workingTreeView.setTreeData(new WorkingTreeModel(workingDirectory));
223  TreeUtil.expandRootBranches(workingTreeView);
224  } catch (ParseException ex) {
225  Logger.getLogger(WorkingDesktop.class.getName()).log(Level.SEVERE, null, ex);
226  }
227  return null;
228  }
229  }.refreshData();
230 
231  }
232 
233  @Override
234  public boolean shutdown(boolean optional) throws Exception {
235  if (window != null) {
236  window.close();
237  }
238  return false;
239  }
240 
241  @Override
242  public void suspend() {
243  }
244 
245  @Override
246  public void resume() {
247  }
248 
249  public static void main(String[] args) {
250  DesktopApplicationContext.main(WorkingDesktop.class, args);
251  }
252 
253  public int getSelection(ButtonGroup group) {
254  Button b = group.getSelection();
255  if(b != null) {
256  return new Integer((String) b.getUserData().get("value"));
257  }
258  return -1;
259  }
260 
261 }
boolean shutdown(boolean optional)
static void main(String[] args)
void startup(Display display, Map< String, String > properties)
void initialize(String bbCreateDir, String bbCreatePath)
static void expandRootBranches(TreeView tree)
Definition: TreeUtil.java:32