KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 10.03.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

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

142   protected void okPressed()
143   {
144     if (boldButton.getSelection())
145       fontStyle = Font.BOLD;
146     
147     if (italicButton.getSelection())
148       fontStyle = fontStyle | Font.ITALIC;
149     
150     int sizeIndex = sizeCombo.getSelectionIndex();
151     int size = Integer.parseInt(sizeCombo.getItem(sizeIndex));
152     
153     int fontIndex = fontCombo.getSelectionIndex();
154     String JavaDoc fontName = fontCombo.getItem(fontIndex);
155     
156     request.setText(text.getText());
157     request.setFontName(fontName);
158     request.setFontSize(size);
159     request.setFontStyle(fontStyle);
160     
161     super.okPressed();
162   }
163 }
164
Popular Tags