]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
da4bae8ca8eea43d1bd19b0f3f174b402608b556
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2014 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.jfreechart.chart.properties;\r
13 \r
14 import org.eclipse.swt.events.ControlEvent;\r
15 import org.eclipse.swt.events.ControlListener;\r
16 import org.eclipse.swt.graphics.Point;\r
17 import org.eclipse.swt.widgets.Composite;\r
18 import org.eclipse.ui.IWorkbenchSite;\r
19 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
20 import org.simantics.db.management.ISessionContext;\r
21 import org.simantics.selectionview.StandardProperties;\r
22 \r
23 /**\r
24  * Tab adjusting the layout depending on its dimensions.\r
25  * @author Tuomas Miettinen\r
26  *\r
27  */\r
28 public abstract class AdjustableTab extends LabelPropertyTabContributor {\r
29 \r
30         private Composite spp;\r
31         private ControlListener controlListener;\r
32         private static final int WIDE_SCREEN_WIDTH = 1100;\r
33         \r
34         @Override\r
35         public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport _support) {\r
36                 // Get size of the available area.\r
37                 spp = body;\r
38                 do {\r
39                         spp = spp.getParent();\r
40                 } while (!(spp instanceof StandardProperties));\r
41                 \r
42                 // Add listener to change the layout when the composite resizes.\r
43                 spp.addControlListener(controlListener = new ControlListener(){\r
44 \r
45                         @Override\r
46                         public void controlMoved(ControlEvent e) {}\r
47 \r
48                         @Override\r
49                         public void controlResized(ControlEvent e) {\r
50                                 createLayout();                         \r
51                         }\r
52                 });\r
53                 \r
54                 // Create the controls and their initial layout.\r
55                 createAndAddControls(body, site, context, _support);\r
56                 createLayout(); \r
57         }\r
58 \r
59         /**\r
60          * Create controls and add them to the tab.\r
61          * @param body the composite where the controls are added.\r
62          * @param site\r
63          * @param context\r
64          * @param _support\r
65          */\r
66         protected abstract void createAndAddControls(Composite body, IWorkbenchSite site,\r
67                         ISessionContext context, WidgetSupport _support);\r
68 \r
69         /**\r
70          *  Create layout for controls.\r
71          */\r
72         protected void createLayout() {\r
73                 Point size = spp.getSize();\r
74                 if (size.x > size.y) {\r
75                         createControlLayoutHorizontal((size.x > WIDE_SCREEN_WIDTH));\r
76                 } else {\r
77                         createControlLayoutVertical();\r
78                 }\r
79         }\r
80         \r
81         /**\r
82          * Determine if the layout uses vertical layout\r
83          * @return true iff the layout uses vertical layout\r
84          */\r
85         protected boolean isVertical() {\r
86                 Point size = spp.getSize();\r
87                 return size.x < size.y;\r
88         }\r
89         \r
90         /**\r
91          * Create vertical layout for controls.\r
92          */\r
93         protected abstract void createControlLayoutVertical();\r
94 \r
95         /**\r
96          * Create horizontal layout for controls.\r
97          * @param wideScreen true iff the control is wider than WIDE_SCREEN_WIDTH\r
98          */\r
99         protected abstract void createControlLayoutHorizontal(boolean wideScreen);\r
100 \r
101         @Override\r
102         public void dispose() {\r
103                 if(controlListener != null && spp != null)\r
104                         spp.removeControlListener(controlListener);\r
105         }\r
106 \r
107 }\r