Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
df0b8ff4 BH |
2 | * Copyright (c) 2005, 2008 IBM Corporation and others. |
3 | * Copyright (c) 2011, 2012 Ericsson. | |
4 | * | |
73005152 BH |
5 | * All rights reserved. This program and the accompanying materials |
6 | * are made available under the terms of the Eclipse Public License v1.0 | |
7 | * which accompanies this distribution, and is available at | |
8 | * http://www.eclipse.org/legal/epl-v10.html | |
73005152 BH |
9 | * |
10 | * Contributors: | |
11 | * IBM - Initial API and implementation | |
12 | * Bernd Hufmann - Updated for TMF | |
13 | **********************************************************************/ | |
df0b8ff4 | 14 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs; |
73005152 BH |
15 | |
16 | import java.text.MessageFormat; | |
17 | ||
18 | import org.eclipse.jface.dialogs.Dialog; | |
19 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDAdvancedPagingProvider; | |
20 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages; | |
21 | import org.eclipse.swt.SWT; | |
22 | import org.eclipse.swt.layout.FillLayout; | |
23 | import org.eclipse.swt.layout.GridData; | |
24 | import org.eclipse.swt.widgets.Composite; | |
25 | import org.eclipse.swt.widgets.Control; | |
26 | import org.eclipse.swt.widgets.Group; | |
27 | import org.eclipse.swt.widgets.Label; | |
28 | import org.eclipse.swt.widgets.Text; | |
29 | import org.eclipse.ui.IViewPart; | |
30 | ||
31 | /** | |
df0b8ff4 BH |
32 | * Class implementation of the pages dialog.<br> |
33 | * | |
34 | * It is associated to an SDView and to a ISDAdvancedPagingProvider.<br> | |
35 | * | |
36 | * @version 1.0 | |
37 | * @author sveyrier | |
73005152 BH |
38 | */ |
39 | public class PagesDialog extends Dialog { | |
40 | ||
df0b8ff4 BH |
41 | // ------------------------------------------------------------------------ |
42 | // Attributes | |
43 | // ------------------------------------------------------------------------ | |
44 | ||
73005152 BH |
45 | /** |
46 | * viewer and provided are kept here as attributes | |
47 | */ | |
48 | protected ISDAdvancedPagingProvider provider = null; | |
49 | protected TextArea currentPage; | |
50 | protected Label totalPageComment; | |
51 | ||
df0b8ff4 BH |
52 | // ------------------------------------------------------------------------ |
53 | // Constructors | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
73005152 | 56 | /** |
df0b8ff4 BH |
57 | * Standard constructor |
58 | * | |
59 | * @param view_ The sequence diagram view reference | |
60 | * @param provider_ The paging provider reference | |
73005152 | 61 | */ |
df0b8ff4 BH |
62 | public PagesDialog(IViewPart view_, ISDAdvancedPagingProvider provider_) { |
63 | super(view_.getSite().getShell()); | |
64 | provider = provider_; | |
65 | setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); | |
73005152 BH |
66 | } |
67 | ||
df0b8ff4 BH |
68 | // ------------------------------------------------------------------------ |
69 | // Methods | |
70 | // ------------------------------------------------------------------------ | |
73005152 | 71 | |
df0b8ff4 BH |
72 | /* |
73 | * (non-Javadoc) | |
73005152 BH |
74 | * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
75 | */ | |
76 | @Override | |
77 | public Control createDialogArea(Composite parent) { | |
78 | ||
79 | Group ret = new Group(parent, SWT.NONE); | |
80 | GridData data = new GridData(); | |
81 | data.grabExcessHorizontalSpace = true; | |
82 | data.horizontalAlignment = GridData.FILL; | |
83 | ret.setLayoutData(data); | |
84 | ret.setText(SDMessages._67); | |
85 | ||
86 | FillLayout fillLayout = new FillLayout(SWT.VERTICAL); | |
87 | ret.setLayout(fillLayout); | |
88 | ||
89 | Label label = new Label(ret, SWT.NONE); | |
90 | label.setText(SDMessages._75); | |
91 | ||
92 | currentPage = new TextArea(ret); | |
93 | currentPage.setBounds(1, provider.pagesCount()); | |
94 | currentPage.setValue(provider.currentPage() + 1); | |
95 | ||
96 | totalPageComment = new Label(ret, SWT.NONE); | |
97 | totalPageComment.setAlignment(SWT.RIGHT); | |
98 | ||
99 | updateComments(); | |
100 | ||
101 | getShell().setText(SDMessages._68); | |
102 | return ret; | |
103 | } | |
104 | ||
df0b8ff4 BH |
105 | /* |
106 | * (non-Javadoc) | |
107 | * @see org.eclipse.jface.dialogs.Dialog#okPressed() | |
73005152 BH |
108 | */ |
109 | @Override | |
110 | public void okPressed() { | |
111 | int currentPageValue = currentPage.getValue() - 1; | |
112 | super.close(); | |
113 | provider.pageNumberChanged(currentPageValue); | |
114 | } | |
115 | ||
df0b8ff4 BH |
116 | /** |
117 | * Updates the comments texts. | |
118 | */ | |
119 | protected void updateComments() { | |
120 | int pages = Math.max(0, provider.pagesCount()); | |
121 | String totalPageCommentText = SDMessages._70 + pages + " "; //$NON-NLS-1$ | |
122 | if (pages == 0) { | |
123 | totalPageCommentText += SDMessages._71; | |
124 | } else if (pages == 1) { | |
125 | totalPageCommentText += SDMessages._72; | |
126 | } else { | |
127 | totalPageCommentText += SDMessages._73; | |
128 | } | |
129 | totalPageComment.setText(totalPageCommentText); | |
130 | } | |
131 | ||
132 | ||
133 | // ------------------------------------------------------------------------ | |
134 | // Helper classes | |
135 | // ------------------------------------------------------------------------ | |
136 | /** | |
137 | * This is a Text Control that accepts only digits and ensures that bounds are respected | |
138 | */ | |
139 | protected static class TextArea { | |
140 | /** | |
141 | * The text field. | |
142 | */ | |
143 | protected Text text; | |
144 | /** | |
145 | * The minimum page value | |
146 | */ | |
147 | int min; | |
148 | /** | |
149 | * The maximum page value | |
150 | */ | |
151 | int max; | |
152 | ||
153 | /** | |
154 | * Constructor | |
155 | * | |
156 | * @param parent The paren composite | |
157 | */ | |
158 | public TextArea(Composite parent) { | |
159 | text = new Text(parent, SWT.SINGLE | SWT.BORDER | SWT.RIGHT); | |
160 | text.setTextLimit(10); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Sets the page value. | |
165 | * | |
166 | * @param page The page value | |
167 | */ | |
168 | public void setValue(int page) { | |
169 | int value = Math.max(min, Math.min(max, page)); | |
170 | text.setText(Integer.toString(value)); | |
171 | } | |
172 | ||
173 | /** | |
174 | * Returns the page value. | |
175 | * | |
176 | * @return the page value | |
177 | */ | |
178 | public int getValue() { | |
179 | int res; | |
180 | try { | |
181 | res = Integer.parseInt(text.getText()); | |
182 | } catch (Exception e) { | |
183 | // ignored | |
184 | res = 0; | |
185 | } | |
186 | return Math.max(min, Math.min(max, res)); | |
187 | } | |
188 | ||
189 | /** | |
190 | * Sets the minimum and maximum page values. | |
191 | * | |
192 | * @param min_ A minimum page value | |
193 | * @param max_ A maximum page value | |
194 | */ | |
195 | public void setBounds(int min_, int max_) { | |
196 | min = Math.max(0, min_); | |
197 | max = Math.max(min, max_); | |
198 | Integer tab[] = new Integer[2]; | |
199 | tab[0] = Integer.valueOf(min); | |
200 | tab[1] = Integer.valueOf(max); | |
201 | text.setToolTipText(MessageFormat.format(SDMessages._69, (Object[]) tab)); | |
202 | } | |
203 | } | |
204 | ||
73005152 | 205 | } |