]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/plugins/org.argeo.slc.client.rcp/src/main/java/org/argeo/slc/client/rcp/SlcSecureWorkbenchWindowAdvisor.java
Clean up directories
[gpl/argeo-slc.git] / legacy / plugins / org.argeo.slc.client.rcp / src / main / java / org / argeo / slc / client / rcp / SlcSecureWorkbenchWindowAdvisor.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.client.rcp;
17
18 import org.argeo.security.ui.rcp.SecureWorkbenchWindowAdvisor;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.dnd.DropTargetAdapter;
21 import org.eclipse.swt.dnd.DropTargetEvent;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.widgets.Event;
24 import org.eclipse.swt.widgets.Listener;
25 import org.eclipse.swt.widgets.Menu;
26 import org.eclipse.swt.widgets.MenuItem;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.swt.widgets.Tray;
29 import org.eclipse.swt.widgets.TrayItem;
30 import org.eclipse.ui.IWorkbenchWindow;
31 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
32 import org.eclipse.ui.part.EditorInputTransfer;
33
34 /**
35 * Custom {@link SecureWorkbenchWindowAdvisor} which can add a tray. TODO: to
36 * be factorized in Argeo Commons and made more generic.
37 */
38 public class SlcSecureWorkbenchWindowAdvisor extends
39 SecureWorkbenchWindowAdvisor {
40 public final static String IN_TRAY_PROPERTY = "org.argeo.slc.ui.inTray";
41
42 private TrayItem trayItem;
43
44 public SlcSecureWorkbenchWindowAdvisor(
45 IWorkbenchWindowConfigurer configurer, String username) {
46 super(configurer, username);
47 }
48
49 public void postWindowOpen() {
50 String inTray = System.getProperty(IN_TRAY_PROPERTY);
51 if (inTray != null && inTray.equals("true")) {
52 initTray();
53 }
54 }
55
56 @Override
57 public void preWindowOpen() {
58 getWindowConfigurer().addEditorAreaTransfer(
59 EditorInputTransfer.getInstance());
60 getWindowConfigurer().configureEditorAreaDropListener(
61 new DropTargetAdapter() {
62
63 @Override
64 public void dragEnter(DropTargetEvent event) {
65 System.out.println("DROP enter!!! " + event);
66 }
67
68 @Override
69 public void dragLeave(DropTargetEvent event) {
70 System.out.println("DROP leave!!! " + event);
71 }
72
73 public void drop(DropTargetEvent event) {
74 System.out.println("DROP drop!!! " + event);
75
76 }
77
78 @Override
79 public void dropAccept(DropTargetEvent event) {
80 System.out.println("DROP accept!!! " + event);
81 super.dropAccept(event);
82 }
83
84 });
85 super.preWindowOpen();
86 }
87
88 @Override
89 public boolean preWindowShellClose() {
90 // hide but do not dispose if tray is supported
91 if (trayItem != null) {
92 getWindowConfigurer().getWindow().getShell().setVisible(false);
93 return false;
94 } else
95 return true;
96 }
97
98 /** Init tray support */
99 protected void initTray() {
100 IWorkbenchWindow window = getWindowConfigurer().getWindow();
101 Shell shell = window.getShell();
102 final Tray tray = shell.getDisplay().getSystemTray();
103 trayItem = new TrayItem(tray, SWT.NONE);
104 if (trayItem == null)
105 return;
106
107 // image
108 Image trayImage = SlcRcpPlugin.getDefault().getImageRegistry()
109 .get("argeoTrayIcon");
110 trayItem.setImage(trayImage);
111 trayItem.setToolTipText("Argeo SLC");
112
113 // add pop-menu
114 // TODO: contribute more commands
115 trayItem.addListener(SWT.MenuDetect, new Listener() {
116 public void handleEvent(Event event) {
117 IWorkbenchWindow window = getWindowConfigurer().getWindow();
118 Menu menu = new Menu(window.getShell(), SWT.POP_UP);
119 MenuItem exit = new MenuItem(menu, SWT.NONE);
120 exit.setText("Exit");
121 exit.addListener(SWT.Selection, new Listener() {
122 public void handleEvent(Event event) {
123 getWindowConfigurer().getWorkbenchConfigurer()
124 .getWorkbench().close();
125 }
126 });
127 menu.setVisible(true);
128 }
129 });
130
131 // add behavior when clicked upon
132 trayItem.addListener(SWT.Selection, new Listener() {
133 public void handleEvent(Event event) {
134 Shell shell = getWindowConfigurer().getWindow().getShell();
135 if (shell.isVisible()) {
136 if (shell.getMinimized())
137 shell.setMinimized(false);
138 else {
139 shell.setVisible(false);
140 shell.setMinimized(true);
141 }
142 } else {
143 shell.setVisible(true);
144 shell.setActive();
145 shell.setFocus();
146 shell.setMinimized(false);
147 }
148 }
149 });
150
151 // start hidden
152 // shell.setVisible(false);
153 }
154
155 @Override
156 public void dispose() {
157 if (trayItem != null)
158 trayItem.dispose();
159 }
160
161 }