KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > dialog > CreateTextDialog


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.dialog;
29
30
31
32 import java.awt.Font JavaDoc;
33
34 import org.eclipse.jface.dialogs.Dialog;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Combo;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.swt.widgets.Text;
45
46 import org.nightlabs.editor2d.EditorPlugin;
47 import org.nightlabs.editor2d.request.TextCreateRequest;
48 import org.nightlabs.util.FontUtil;
49
50 public class CreateTextDialog
51 extends Dialog
52 {
53   protected TextCreateRequest request;
54   
55   public CreateTextDialog(Shell parentShell, TextCreateRequest request)
56   {
57     super(parentShell);
58     this.request = request;
59   }
60    
61   protected String JavaDoc defaultFontName = new String JavaDoc("Arial");
62   protected int defaultSize = 24;
63   
64   protected int style = SWT.NONE;
65   protected Combo fontCombo;
66   protected Text text;
67   protected Combo sizeCombo;
68   protected Button italicButton;
69   protected Button boldButton;
70   
71   protected int fontStyle = Font.PLAIN;
72 // protected int[] sizeArray = new int[] {8,10,12,14,16,18,24,30,36};
73
protected String JavaDoc[] fonts;
74   
75   protected Composite dialogComp;
76   protected Control createDialogArea(Composite parent)
77   {
78     getShell().setText(EditorPlugin.getResourceString("dialog.createText.title"));
79     
80     dialogComp = new Composite(parent, style);
81     GridLayout layout = new GridLayout();
82     dialogComp.setLayout(layout);
83
84     Composite nameComp = new Composite(dialogComp, style);
85     nameComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
86     GridLayout nameLayout = new GridLayout();
87     nameLayout.numColumns = 2;
88     nameLayout.marginWidth = 0;
89     nameComp.setLayout(nameLayout);
90     
91     Label nameLabel = new Label(nameComp, style);
92     nameLabel.setText(EditorPlugin.getResourceString("label.name"));
93     
94     // TODO: Why is this text so short ?????
95
text = new Text(nameComp, style | SWT.BORDER);
96 // GridData textData = new GridData(GridData.FILL_BOTH);
97
GridData textData = new GridData();
98     textData.horizontalAlignment = GridData.FILL;
99     textData.grabExcessHorizontalSpace = true;
100     text.setLayoutData(textData);
101     
102     Composite detailComp = new Composite(dialogComp, style);
103     GridLayout detailLayout = new GridLayout();
104     detailLayout.numColumns = 4;
105     detailLayout.marginWidth = 0;
106     detailComp.setLayout(detailLayout);
107     
108     createFontCombo(detailComp);
109     createSizeCombo(detailComp);
110     
111     boldButton = new Button(detailComp, style | SWT.TOGGLE);
112     boldButton.setText("B");
113     
114     italicButton = new Button(detailComp, style | SWT.TOGGLE);
115     italicButton.setText("I");
116     
117     return dialogArea;
118   }
119     
120   
121   protected void createFontCombo(Composite parent)
122   {
123     fontCombo = new Combo(parent, style | SWT.READ_ONLY);
124     GridData gridData = new GridData(GridData.FILL_BOTH);
125     gridData.grabExcessHorizontalSpace = true;
126     fontCombo.setLayoutData(gridData);
127     
128     fonts = FontUtil.getSystemFonts();
129     fontCombo.setItems(fonts);
130     
131     for (int i=0; i<fonts.length; i++) {
132       String JavaDoc f = fonts[i];
133       if (f.equals(defaultFontName)) {
134         fontCombo.select(i);
135       }
136     }
137   }
138   
139   protected void createSizeCombo(Composite parent)
140   {
141     sizeCombo = new Combo(parent, style | SWT.READ_ONLY);
142     String JavaDoc[] sizes = FontUtil.getFontSizes();
143     sizeCombo.setItems(sizes);
144     
145     for (int i=0; i<sizes.length; i++) {
146       String JavaDoc f = sizes[i];
147       if (f.equals(Integer.toString(defaultSize))) {
148         sizeCombo.select(i);
149       }
150     }
151   }
152   
153 // protected String[] createSizeArray()
154
// {
155
// String[] stringSizes = new String[sizeArray.length];
156
// for (int i=0; i<sizeArray.length; i++) {
157
// stringSizes[i] = Integer.toString(sizeArray[i]);
158
// }
159
// return stringSizes;
160
// }
161

162   protected void okPressed()
163   {
164     if (boldButton.getSelection())
165       fontStyle = Font.BOLD;
166     
167     if (italicButton.getSelection())
168       fontStyle = fontStyle | Font.ITALIC;
169     
170     int sizeIndex = sizeCombo.getSelectionIndex();
171     int size = Integer.parseInt(sizeCombo.getItem(sizeIndex));
172     
173     int fontIndex = fontCombo.getSelectionIndex();
174     String JavaDoc fontName = fontCombo.getItem(fontIndex);
175     
176     request.setText(text.getText());
177     request.setFontName(fontName);
178     request.setFontSize(size);
179     request.setFontStyle(fontStyle);
180     
181     super.okPressed();
182   }
183 }
184
Popular Tags