⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 generalfilepropertiespage.java

📁 基于Eclipse RCP开发的管理工具
💻 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.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
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.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.s10r.manager.EntryEditor;

public class GeneralFilePropertiesPage extends PreferencePage implements
		IWorkbenchPreferencePage
{
	private EntryEditor activeEditor;

	private Text passwordText;

	private Text confirmPasswordText;

	public GeneralFilePropertiesPage(EntryEditor activeEditor)
	{
		super("General");

		this.activeEditor = activeEditor;
	}

	public void init(IWorkbench workbench)
	{
	}

	@Override
	protected Control createContents(Composite parent)
	{
		// TODO: use form layout instead?

		Composite content = new Composite(parent, SWT.NONE);
		GridLayout gridLayout = new GridLayout();

		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 0;

		content.setLayout(gridLayout);

		GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		content.setLayoutData(data);

		createBasicInfoGroup(content);

		createSeparator(content);

		createPasswordGroup(content);

		createSeparator(content);

		createEncryptionGroup(content);

		return content;
	}

	private void createEncryptionGroup(Composite content)
	{
		Composite composite = new Composite(content, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		composite.setLayout(layout);

		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);

		/*
		 * Label pathLabel = new Label(composite, SWT.NONE);
		 * pathLabel.setText("TODO: add encryption parameters"); GridData gd =
		 * new GridData(); gd.verticalAlignment = SWT.CENTER;
		 * pathLabel.setLayoutData(gd);
		 */

	}

	private void createPasswordGroup(Composite content)
	{
		Composite composite = new Composite(content, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		composite.setLayout(layout);

		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);

		Label pathLabel = new Label(composite, SWT.NONE);
		pathLabel.setText("Password: ");
		GridData gd = new GridData();
		gd.verticalAlignment = SWT.CENTER;
		pathLabel.setLayoutData(gd);

		passwordText = new Text(composite, SWT.PASSWORD | SWT.BORDER);
		passwordText.setText(activeEditor.getEntryEditorInput().getPassword());
		gd = new GridData();
		gd.grabExcessHorizontalSpace = true;
		gd.horizontalAlignment = GridData.FILL;
		passwordText.setLayoutData(gd);

		Label sizeLabel = new Label(composite, SWT.NONE);
		sizeLabel.setText("Confirm Password: ");
		gd = new GridData();
		gd.verticalAlignment = SWT.CENTER;
		sizeLabel.setLayoutData(gd);

		confirmPasswordText = new Text(composite, SWT.PASSWORD | SWT.BORDER);
		confirmPasswordText.setText(activeEditor.getEntryEditorInput()
				.getPassword());
		gd = new GridData();
		gd.grabExcessHorizontalSpace = true;
		gd.horizontalAlignment = GridData.FILL;
		confirmPasswordText.setLayoutData(gd);

		KeyListener keyListener = new KeyListener()
		{
			public void keyPressed(KeyEvent e)
			{
			}

			public void keyReleased(KeyEvent e)
			{
				if (!isPasswordValid())
				{
					setErrorMessage("Password may not be empty. The password field must "
							+ "match the confirm password field.");
					setValid(false); // disables the apply and ok button
				}
				else
				{
					setErrorMessage(null);
					setValid(true);
				}
			}
		};

		passwordText.addKeyListener(keyListener);
		confirmPasswordText.addKeyListener(keyListener);
	}

	private boolean isPasswordValid()
	{
		String pw = passwordText.getText();
		String cpw = confirmPasswordText.getText();

		if (pw == null || cpw == null)
		{
			return false;
		}
		else
		{
			return pw.equals(cpw);
		}
	}

	private void createBasicInfoGroup(Composite content)
	{
		Composite composite = new Composite(content, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		composite.setLayout(layout);

		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);

		Label pathLabel = new Label(composite, SWT.NONE);
		pathLabel.setText("Path: ");
		GridData gd = new GridData();
		gd.verticalAlignment = SWT.TOP;
		pathLabel.setLayoutData(gd);

		Text pathValueText = new Text(composite, SWT.WRAP | SWT.READ_ONLY);
		pathValueText.setText(activeEditor.getEntryEditorInput().getPath());
		gd = new GridData();
		gd.grabExcessHorizontalSpace = true;
		gd.horizontalAlignment = GridData.FILL;
		pathValueText.setLayoutData(gd);

		Label sizeLabel = new Label(composite, SWT.NONE);
		sizeLabel.setText("Size: ");
		gd = new GridData();
		gd.verticalAlignment = SWT.TOP;
		sizeLabel.setLayoutData(gd);

		Text sizeValueText = new Text(composite, SWT.WRAP | SWT.READ_ONLY);
		sizeValueText.setText(""
				+ activeEditor.getEntryEditorInput().getFile().length()
				+ " bytes");
		gd = new GridData();
		gd.grabExcessHorizontalSpace = true;
		gd.horizontalAlignment = GridData.FILL;
		sizeValueText.setLayoutData(gd);
	}

	private void createSeparator(Composite composite)
	{

		Label separator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		separator.setLayoutData(gridData);
	}

	@Override
	protected void performApply()
	{
		super.performApply();

		String password = passwordText.getText();
		// this should never happen as the validation of the page checks
		// for empty/null passwords.
		if (password == null)
		{
			throw new IllegalArgumentException("Password may not be null!");
		}

		if (!password.equals(activeEditor.getEntryEditorInput().getPassword()))
		{
			activeEditor.getEntryEditorInput().setPassword(password);
			activeEditor.doSave(null);
		}
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -