]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.rcp/src/main/java/org/argeo/slc/client/rcp/SlcSecureWorkbenchWindowAdvisor.java
Move to SLC legacy
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.rcp / src / main / java / org / argeo / slc / client / rcp / SlcSecureWorkbenchWindowAdvisor.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 /** Set here initial default size of the UI */
35 public class SlcSecureWorkbenchWindowAdvisor extends
36 SecureWorkbenchWindowAdvisor {
37 public final static String IN_TRAY_PROPERTY = "org.argeo.slc.ui.inTray";
38
39 private TrayItem trayItem;
40
41 public SlcSecureWorkbenchWindowAdvisor(
42 IWorkbenchWindowConfigurer configurer, String username) {
43 super(configurer, username);
44 }
45
46 public void postWindowOpen() {
47 String inTray = System.getProperty(IN_TRAY_PROPERTY);
48 if (inTray != null && inTray.equals("true")) {
49 initTray();
50 }
51 }
52
53 @Override
54 public void preWindowOpen() {
55 getWindowConfigurer().addEditorAreaTransfer(
56 EditorInputTransfer.getInstance());
57 getWindowConfigurer().configureEditorAreaDropListener(
58 new DropTargetAdapter() {
59
60 @Override
61 public void dragEnter(DropTargetEvent event) {
62 System.out.println("DROP enter!!! " + event);
63 }
64
65 @Override
66 public void dragLeave(DropTargetEvent event) {
67 System.out.println("DROP leave!!! " + event);
68 }
69
70 public void drop(DropTargetEvent event) {
71 System.out.println("DROP drop!!! " + event);
72
73 }
74
75 @Override
76 public void dropAccept(DropTargetEvent event) {
77 System.out.println("DROP accept!!! " + event);
78 super.dropAccept(event);
79 }
80
81 });
82 super.preWindowOpen();
83 }
84
85 @Override
86 public boolean preWindowShellClose() {
87 // hide but do not dispose if tray is supported
88 if (trayItem != null) {
89 getWindowConfigurer().getWindow().getShell().setVisible(false);
90 return false;
91 } else
92 return true;
93 }
94
95 /** Init tray support */
96 protected void initTray() {
97 IWorkbenchWindow window = getWindowConfigurer().getWindow();
98 Shell shell = window.getShell();
99 final Tray tray = shell.getDisplay().getSystemTray();
100 trayItem = new TrayItem(tray, SWT.NONE);
101 if (trayItem == null)
102 return;
103
104 // image
105 Image trayImage = ClientRcpPlugin.getDefault().getImageRegistry()
106 .get("argeoTrayIcon");
107 trayItem.setImage(trayImage);
108 trayItem.setToolTipText("Argeo SLC");
109
110 // add pop-menu
111 // TODO: contribute more commands
112 trayItem.addListener(SWT.MenuDetect, new Listener() {
113 public void handleEvent(Event event) {
114 IWorkbenchWindow window = getWindowConfigurer().getWindow();
115 Menu menu = new Menu(window.getShell(), SWT.POP_UP);
116 MenuItem exit = new MenuItem(menu, SWT.NONE);
117 exit.setText("Exit");
118 exit.addListener(SWT.Selection, new Listener() {
119 public void handleEvent(Event event) {
120 getWindowConfigurer().getWorkbenchConfigurer()
121 .getWorkbench().close();
122 }
123 });
124 menu.setVisible(true);
125 }
126 });
127
128 // add behavior when clicked upon
129 trayItem.addListener(SWT.Selection, new Listener() {
130 public void handleEvent(Event event) {
131 Shell shell = getWindowConfigurer().getWindow().getShell();
132 if (shell.isVisible()) {
133 if (shell.getMinimized())
134 shell.setMinimized(false);
135 else {
136 shell.setVisible(false);
137 shell.setMinimized(true);
138 }
139 } else {
140 shell.setVisible(true);
141 shell.setActive();
142 shell.setFocus();
143 shell.setMinimized(false);
144 }
145 }
146 });
147
148 // start hidden
149 // shell.setVisible(false);
150 }
151
152 @Override
153 public void dispose() {
154 if (trayItem != null)
155 trayItem.dispose();
156 }
157
158 }