0d11478be8c7499a83da2a356eb6f643f638d15d
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / dialogs / PagesDialog.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
4 *
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
9 *
10 * Contributors:
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14 package org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs;
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 /**
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
38 */
39 public class PagesDialog extends Dialog {
40
41 // ------------------------------------------------------------------------
42 // Attributes
43 // ------------------------------------------------------------------------
44
45 /**
46 * viewer and provided are kept here as attributes
47 */
48 protected ISDAdvancedPagingProvider fProvider = null;
49
50 /** Current page */
51 protected TextArea fCurrentPage;
52
53 /** Comment label */
54 protected Label fTotalPageComment;
55
56 // ------------------------------------------------------------------------
57 // Constructors
58 // ------------------------------------------------------------------------
59
60 /**
61 * Standard constructor
62 *
63 * @param view The sequence diagram view reference
64 * @param provider The paging provider reference
65 */
66 public PagesDialog(IViewPart view, ISDAdvancedPagingProvider provider) {
67 super(view.getSite().getShell());
68 fProvider = provider;
69 setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
70 }
71
72 // ------------------------------------------------------------------------
73 // Methods
74 // ------------------------------------------------------------------------
75
76 /*
77 * (non-Javadoc)
78 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
79 */
80 @Override
81 public Control createDialogArea(Composite parent) {
82
83 Group ret = new Group(parent, SWT.NONE);
84 GridData data = new GridData();
85 data.grabExcessHorizontalSpace = true;
86 data.horizontalAlignment = GridData.FILL;
87 ret.setLayoutData(data);
88 ret.setText(SDMessages._67);
89
90 FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
91 ret.setLayout(fillLayout);
92
93 Label label = new Label(ret, SWT.NONE);
94 label.setText(SDMessages._75);
95
96 fCurrentPage = new TextArea(ret);
97 fCurrentPage.setBounds(1, fProvider.pagesCount());
98 fCurrentPage.setValue(fProvider.currentPage() + 1);
99
100 fTotalPageComment = new Label(ret, SWT.NONE);
101 fTotalPageComment.setAlignment(SWT.RIGHT);
102
103 updateComments();
104
105 getShell().setText(SDMessages._68);
106 return ret;
107 }
108
109 /*
110 * (non-Javadoc)
111 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
112 */
113 @Override
114 public void okPressed() {
115 int currentPageValue = fCurrentPage.getValue() - 1;
116 super.close();
117 fProvider.pageNumberChanged(currentPageValue);
118 }
119
120 /**
121 * Updates the comments texts.
122 */
123 protected void updateComments() {
124 int pages = Math.max(0, fProvider.pagesCount());
125 StringBuffer totalPageCommentText = new StringBuffer();
126 totalPageCommentText.append(SDMessages._70);
127 totalPageCommentText.append(pages);
128 totalPageCommentText.append(" "); //$NON-NLS-1$
129 if (pages == 0) {
130 totalPageCommentText.append(SDMessages._71);
131 } else if (pages == 1) {
132 totalPageCommentText.append(SDMessages._72);
133 } else {
134 totalPageCommentText.append(SDMessages._73);
135 }
136 fTotalPageComment.setText(totalPageCommentText.toString());
137 }
138
139
140 // ------------------------------------------------------------------------
141 // Helper classes
142 // ------------------------------------------------------------------------
143 /**
144 * This is a Text Control that accepts only digits and ensures that bounds are respected
145 */
146 protected static class TextArea {
147 /**
148 * The text field.
149 */
150 protected Text fText;
151 /**
152 * The minimum page value
153 */
154 int fMin;
155 /**
156 * The maximum page value
157 */
158 int fMax;
159
160 /**
161 * Constructor
162 *
163 * @param parent The paren composite
164 */
165 public TextArea(Composite parent) {
166 fText = new Text(parent, SWT.SINGLE | SWT.BORDER | SWT.RIGHT);
167 fText.setTextLimit(10);
168 }
169
170 /**
171 * Sets the page value.
172 *
173 * @param page The page value
174 */
175 public void setValue(int page) {
176 int value = Math.max(fMin, Math.min(fMax, page));
177 fText.setText(Integer.toString(value));
178 }
179
180 /**
181 * Returns the page value.
182 *
183 * @return the page value
184 */
185 public int getValue() {
186 int res;
187 try {
188 res = Integer.parseInt(fText.getText());
189 } catch (Exception e) {
190 // ignored
191 res = 0;
192 }
193 return Math.max(fMin, Math.min(fMax, res));
194 }
195
196 /**
197 * Sets the minimum and maximum page values.
198 *
199 * @param min A minimum page value
200 * @param max A maximum page value
201 */
202 public void setBounds(int min, int max) {
203 fMin = Math.max(0, min);
204 fMax = Math.max(fMin, max);
205 Integer tab[] = new Integer[2];
206 tab[0] = Integer.valueOf(fMin);
207 tab[1] = Integer.valueOf(fMax);
208 fText.setToolTipText(MessageFormat.format(SDMessages._69, (Object[]) tab));
209 }
210 }
211
212 }
This page took 0.034218 seconds and 5 git commands to generate.