OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / eclipse / plugins / com.android.ide.eclipse.adt / src / com / android / ide / eclipse / adt / internal / editors / layout / gle2 / PropertySheetPage2.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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
17 package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.graphics.Rectangle;
22 import org.eclipse.swt.layout.FillLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Listener;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Tree;
30 import org.eclipse.swt.widgets.TreeItem;
31 import org.eclipse.ui.views.properties.PropertySheetEntry;
32 import org.eclipse.ui.views.properties.PropertySheetPage;
33
34 /**
35  * A customized property sheet page for the graphical layout editor v2.
36  * <p/>
37  * Currently it just provides a custom tooltip to display attributes javadocs.
38  * <p/>
39  * The property sheet is linked to the current site's selection service.
40  * <p/>
41  * Note: this is an exact copy of GLE1's UiPropertySheetPage implementation.
42  * The idea is that eventually GLE1 will go away and we'll upgrade this to be
43  * a more robust property editor (it currently lacks on so many levels, it's not
44  * even worth listing the flaws.)
45  *
46  * @since GLE2
47  */
48 public class PropertySheetPage2 extends PropertySheetPage {
49
50
51     public PropertySheetPage2() {
52         super();
53     }
54
55     @Override
56     public void createControl(Composite parent) {
57         super.createControl(parent);
58
59         setupTooltip();
60     }
61
62     /**
63      * Sets up a custom tooltip when hovering over tree items.
64      * <p/>
65      * The tooltip will display the element's javadoc, if any, or the item's getText otherwise.
66      */
67     private void setupTooltip() {
68         final Tree tree = (Tree) getControl();
69
70         /*
71          * Reference:
72          * http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet125.java?view=markup
73          */
74
75         final Listener listener = new Listener() {
76             Shell tip = null;
77             Label label  = null;
78
79             public void handleEvent(Event event) {
80                 switch(event.type) {
81                 case SWT.Dispose:
82                 case SWT.KeyDown:
83                 case SWT.MouseExit:
84                 case SWT.MouseDown:
85                 case SWT.MouseMove:
86                     if (tip != null) {
87                         tip.dispose();
88                         tip = null;
89                         label = null;
90                     }
91                     break;
92                 case SWT.MouseHover:
93                     if (tip != null) {
94                         tip.dispose();
95                         tip = null;
96                         label = null;
97                     }
98
99                     String tooltip = null;
100
101                     TreeItem item = tree.getItem(new Point(event.x, event.y));
102                     if (item != null) {
103                         Object data = item.getData();
104                         if (data instanceof PropertySheetEntry) {
105                             tooltip = ((PropertySheetEntry) data).getDescription();
106                         }
107
108                         if (tooltip == null) {
109                             tooltip = item.getText();
110                         } else {
111                             tooltip = item.getText() + ":\r" + tooltip;
112                         }
113
114                         if (tooltip != null) {
115                             Shell shell = tree.getShell();
116                             Display display = tree.getDisplay();
117
118                             tip = new Shell(shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
119                             tip.setBackground(display .getSystemColor(SWT.COLOR_INFO_BACKGROUND));
120                             FillLayout layout = new FillLayout();
121                             layout.marginWidth = 2;
122                             tip.setLayout(layout);
123                             label = new Label(tip, SWT.NONE);
124                             label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
125                             label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
126                             label.setData("_TABLEITEM", item);
127                             label.setText(tooltip);
128                             label.addListener(SWT.MouseExit, this);
129                             label.addListener(SWT.MouseDown, this);
130                             Point size = tip.computeSize(SWT.DEFAULT, SWT.DEFAULT);
131                             Rectangle rect = item.getBounds(0);
132                             Point pt = tree.toDisplay(rect.x, rect.y);
133                             tip.setBounds(pt.x, pt.y, size.x, size.y);
134                             tip.setVisible(true);
135                         }
136                     }
137                 }
138             }
139         };
140
141         tree.addListener(SWT.Dispose, listener);
142         tree.addListener(SWT.KeyDown, listener);
143         tree.addListener(SWT.MouseMove, listener);
144         tree.addListener(SWT.MouseHover, listener);
145
146     }
147
148 }