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

📄 classpathfieldeditor.java

📁 一个学习eclipse插件开发的绝好入门程序
💻 JAVA
字号:
package com.sysdeo.eclipse.tomcat.editors;

/*
 * (c) Copyright Sysdeo SA 2001, 2002.
 * All Rights Reserved.
 */

/**
 *	Use in TomcatPreferencePage
 * 	This class is based on PathEditor
 *  There is a button to add files on the list,
 */

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Widget;

import com.sysdeo.eclipse.tomcat.TomcatPluginResources;

/**
 * A field editor to edit directory paths.
 */
public class ClasspathFieldEditor extends ListFieldEditor implements TomcatPluginResources {

	protected Button addJarZipButton;
	protected Button addDirButton;

	/**
	 * The last path, or <code>null</code> if none.
	 */
	private String lastPath;

	/**
	 * Creates a new path field editor 
	 */
	protected ClasspathFieldEditor() {
	}


	public ClasspathFieldEditor(String name, String labelText, Composite parent) {
		init(name, labelText);
		createControl(parent);
	}

	protected String[] getNewJarZip() {
		FileDialog dialog = new FileDialog(addJarZipButton.getShell(), SWT.MULTI);
		if (lastPath != null) {
			if (new File(lastPath).exists())
				dialog.setFilterPath(lastPath);
		}
		String file = dialog.open();
	
		if (dialog.getFileNames().length != 0) {
			lastPath = dialog.getFilterPath();
			String[] result = dialog.getFileNames();
			for (int i = 0; i < result.length; i++) {
				result[i] = lastPath + File.separator + result[i];
			}
			return result;
		} else {
			return new String[0];
		}		
	}

	protected String getNewDir() {
		DirectoryDialog dialog = new DirectoryDialog(addDirButton.getShell());
		if (lastPath != null) {
			if (new File(lastPath).exists())
				dialog.setFilterPath(lastPath);
		}
		String dir = dialog.open();
		if (dir != null) {
			dir = dir.trim();
			if (dir.length() == 0)
				return null;
			lastPath = dir;
		}
		return dir;
	}

	protected void createButtons(Composite buttonBox) {
		addJarZipButton = createPushButton(buttonBox, PREF_PAGE_ADDJARZIPBUTTON_LABEL);//$NON-NLS-1$
		addDirButton = createPushButton(buttonBox, PREF_PAGE_ADDDIRBUTTON_LABEL);//$NON-NLS-1$
		removeButton = createPushButton(buttonBox, PREF_PAGE_REMOVEBUTTON_LABEL);//$NON-NLS-1$
		upButton = createPushButton(buttonBox, PREF_PAGE_UPBUTTON_LABEL);//$NON-NLS-1$
		downButton = createPushButton(buttonBox, PREF_PAGE_DOWNBUTTON_LABEL);//$NON-NLS-1$
	}

	/**
	 * Creates a selection listener.
	 */
	public void createSelectionListener() {
		selectionListener = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				Widget widget = event.widget;
				if (widget == addJarZipButton) {
					addJarZipPressed();
				} else
					if (widget == addDirButton) {
						addDirPressed();
					} else
						if (widget == removeButton) {
							removePressed();
						} else
							if (widget == upButton) {
								upPressed();
							} else
								if (widget == downButton) {
									downPressed();
								} else
									if (widget == list) {
										selectionChanged();
									}
			}
		};
	}

	protected void addJarZipPressed() {
		setPresentsDefaultValue(false);
		String[] input = getNewJarZip();
	
		for(int i=0; i<input.length; i++) {
			if (input != null) {
				int index = list.getSelectionIndex();
				if (index >= 0)
					list.add(input[i], index + 1);
				else
					list.add(input[i], 0);
				selectionChanged();
			}
		}
	}
	
	
	protected void addDirPressed() {
		setPresentsDefaultValue(false);
		String input = getNewDir();
	
		if (input != null) {
			int index = list.getSelectionIndex();
			if (index >= 0)
				list.add(input, index + 1);
			else
				list.add(input, 0);
			selectionChanged();
		}
	}


}

⌨️ 快捷键说明

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