📄 editlabeldialog.java
字号:
/* * Copyright 2006 Marcel Schoffelmeer * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.s10r.manager.dialogs;import org.eclipse.jface.dialogs.IDialogConstants;import org.eclipse.jface.dialogs.TitleAreaDialog;import org.eclipse.swt.SWT;import org.eclipse.swt.events.KeyEvent;import org.eclipse.swt.events.KeyListener;import org.eclipse.swt.graphics.RGB;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Text;import com.s10r.manager.model.ItemLabel;import com.s10r.util.DuplicateChecker;/** * TODO: remember what settings are used to generate passwords * * @author schoffem */public class EditLabelDialog extends TitleAreaDialog{ private ItemLabel label; private Text nameText; private Text descriptionText; private DialogType dialogType; private DuplicateChecker duplicateChecker; public EditLabelDialog(Shell shell, DuplicateChecker duplicateChecker, DialogType dialogType) { super(shell); this.duplicateChecker = duplicateChecker; this.dialogType = dialogType; setShellStyle(SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM | SWT.RESIZE); setTitleAreaColor(new RGB(255, 255, 255)); } @Override public void create() { super.create(); this.setTitle("Label"); if (dialogType == DialogType.CREATE) { this.getShell().setText("New Label"); this.setMessage("Create a new label"); } else if (dialogType == DialogType.UPDATE) { this.getShell().setText("Edit Label"); this.setMessage("Edit a label"); } } @Override protected Control createDialogArea(Composite parent) { final Composite area = new Composite(parent, SWT.NONE); // the parent uses a grid layout // so set the area layout data to fill in both // directions GridData data2 = new GridData(GridData.FILL_BOTH); area.setLayoutData(data2); final GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 3; gridLayout.marginWidth = 15; gridLayout.marginHeight = 10; gridLayout.makeColumnsEqualWidth = false; area.setLayout(gridLayout); newLabel(area, "Name:"); nameText = new Text(area, SWT.BORDER); nameText.setLayoutData(createTextGridData()); Label descLabel = newLabel(area, "Description:"); descLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); descriptionText = new Text(area, SWT.BORDER | SWT.MULTI); descriptionText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 3)); // fill up the blanks on the left side of the description field newLabel(area, ""); newLabel(area, ""); KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent arg0) { } public void keyReleased(KeyEvent arg0) { validateEntry(); } }; nameText.addKeyListener(keyListener); descriptionText.addKeyListener(keyListener); transferEntryToFields(); return area; } private void transferEntryToFields() { setTextField(nameText, label.getName()); setTextField(descriptionText, label.getDescription()); } private void transferFieldsToEntry() { label.setName(nameText.getText()); label.setDescription(descriptionText.getText()); } private void setTextField(Text text, String s) { if (s != null) { text.setText(s); } } private Label newLabel(Composite parent, String text) { Label label = new Label(parent, SWT.NONE); label.setText(text); label.setLayoutData(new GridData(SWT.LEFT, SWT.LEFT, false, false)); return label; } private GridData createTextGridData() { return createTextGridData(2, 1); } private GridData createTextGridData(int horizontalSpan, int verticalSpan) { return new GridData(SWT.FILL, SWT.FILL, true, false, horizontalSpan, verticalSpan); } @Override protected Control createButtonBar(Composite arg0) { Control c = super.createButtonBar(arg0); enableOK(false); return c; } @Override protected void okPressed() { transferFieldsToEntry(); super.okPressed(); } public void setEntry(ItemLabel entry) { this.label = entry; } private void validateEntry() { enableOK(false); if (nameText.getText().length() == 0) { setErrorMessage("The name may not be empty."); } else if (duplicateChecker != null && duplicateChecker.exists(nameText.getText())) { setErrorMessage("Label named " + nameText.getText() + " already exists"); } else { setErrorMessage(null); enableOK(true); } } private void enableOK(boolean enable) { getButton(IDialogConstants.OK_ID).setEnabled(enable); } public ItemLabel getEntry() { return label; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -