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