1 27 28 package org.nightlabs.editor2d.dialog; 29 30 31 32 import java.awt.Font ; 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 defaultFontName = new String ("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 String [] 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 text = new Text(nameComp, style | SWT.BORDER); 96 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 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 [] sizes = FontUtil.getFontSizes(); 143 sizeCombo.setItems(sizes); 144 145 for (int i=0; i<sizes.length; i++) { 146 String f = sizes[i]; 147 if (f.equals(Integer.toString(defaultSize))) { 148 sizeCombo.select(i); 149 } 150 } 151 } 152 153 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 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 |