[HOME]   その他の虎の巻   [Java 一般]   [Java Webアプリケーション]   [Ajax]   [SQL]   [UnitTest]  

Java Eclipse SWF JFACE RCP ZEST GEF DRAW2D 虎の巻

[RCP]ステータスバーをHackする
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
	private static StatusLineManager sm;
	static CLabel label;

	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		sm = (StatusLineManager) configurer.getStatusLineManager();
		return new ApplicationActionBarAdvisor(configurer);
	}

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setShowStatusLine(true);
	}

	// ステータスバーの背景を変えるときにコールする
	public static void setStatusImage(Color[] colors, int[] percents) {
		if (label != null) {
			label.setBackground(colors, percents);
		}
	}

	public static void setStatusImage(Image image) {
		label.setBackground(image);
	}

	// このタイミングでステータスバーを構成するCLabelオブジェクトを取得する
	public void postWindowOpen() {
		try {
			Control con = sm.getControl();
			Field field = con.getClass().getDeclaredField("fMessageLabel");
			field.setAccessible(true);
			label = (CLabel) field.get(con);
		} catch (Exception e) {
			e.printStackTrace();
		}
		setStatusImage(PreferenceUtil.getStatusImage());
	}

}
		
[RCP]意図的にビューを切り離す[Detache View Part]
	IPreferenceStore store = WorkbenchPlugin.getDefault()
			.getPreferenceStore();
	// Now View Mode
	int openViewMode = store
			.getInt(IPreferenceConstants.OPEN_VIEW_MODE);
		// Detache Part
	store.setValue(IPreferenceConstants.OPEN_VIEW_MODE,
				IPreferenceConstants.OVM_FLOAT);
	ViewPart part = getViewPart();
	part.showBusy(true);
	// Reset View Mode
	store.setValue(IPreferenceConstants.OPEN_VIEW_MODE, openViewMode);
		
[RCP]タブをクールに表示する[setSimple Tab]
//今まではこんな感じでした
	private void setNotSimpleTab(IFolderLayout layout) {
		Class clazz = layout.getClass();
		Field f = clazz.getDeclaredField("folder");
		f.setAccessible(true);

		final ViewStack o = (ViewStack) f.get(layout);
		final Method m = PartStack.class
				.getDeclaredMethod("getPresentation");
		m.setAccessible(true);

		Display.getCurrent().timerExec(120, new Thread() {
			@Override
			public void run() {
				StackPresentation sp = (StackPresentation) m
						.invoke(o);
				if (sp instanceof TabbedStackPresentation) {
					TabbedStackPresentation stack =
					 (TabbedStackPresentation) sp;
						AbstractTabFolder tabFolder = stack
							.getTabFolder();
						if (tabFolder instanceof DefaultTabFolder) {
						((DefaultTabFolder) tabFolder)
								.setSimpleTabs(false);
					}
				}
			}
		}

		);
	}
		
	
//Eclipse Ver3.0以降はもっと簡単に出来ます。
	PlatformUI.getPreferenceStore().setValue(
		IWorkbenchPreferenceConstants.
		SHOW_TRADITIONAL_STYLE_TABS, false
	);
		
[Ant]Eclipseから起動したAntが吐いた例外情報の詳細を知りたい
// Antの中で発生した例外がEclipseから起動するとよくわからん。。。
Buildfile: C:\Hoge\test\test.xml
test:
BUILD FAILED
C:\Hoge\test\test.xml:13: java.lang.NullPointerException
Total time: 485 milliseconds
		
// DOSから直接Antをコールするとよくわかる。。。
test>ant -f test.xml
Buildfile: test.xml

test:
BUILD FAILED
java.lang.NullPointerException
        at org.apache.tools.ant.types.resources.FileResourceIterator.addFiles(FileResourceIterator.java:65)
        at org.apache.tools.ant.types.resources.FileResourceIterator.<init>(FileResourceIterator.java:56)
        at org.apache.tools.ant.types.Path$PathElement.iterator(Path.java:124)
        at org.apache.tools.ant.types.resources.Union.getCollection(Union.java:110)
        at org.apache.tools.ant.types.resources.Union.getCollection(Union.java:90)
		
[RCP]任意のページIDを指定して、プリファレンスダイアログを表示する
	public static int openPreferenceDialog(String pageId) {
		PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(null, pageId, null, null);
		return dialog.open();
	}
		
[RCP]Project Explorer and ResouceNavigator, nothing shown
	// RCPのアプリケーションでProjectエクスプローラーや、リソースナビゲータから
	// プロジェクトを作ってもファイルを作っても見えません。
	// ApplicationWorkbenchAdvisor#preStartupのタイミングで
	// WorkbenchAdapterBuilder.registerAdapters()を実行し、かつ、
	// initializeのタイミングで各種リソースのImageDescriptorを登録する必要があります。

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

	private static final String PERSPECTIVE_ID = "wolfdbmanager.perspective";

	public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
		return new ApplicationWorkbenchWindowAdvisor(configurer);
	}

	@Override
	public void initialize(IWorkbenchConfigurer configurer) {
		super.initialize(configurer);
		ImageDescriptor imageDesc = PreferenceUtil.getToolBarImageDescriptor("action/32接続.gif");
		configurer.declareImage(IDE.SharedImages.IMG_OBJ_PROJECT, imageDesc, true);
		configurer.declareImage(IDEInternalWorkbenchImages.IMG_ETOOL_PROBLEM_CATEGORY, imageDesc, true);
		configurer.declareImage(IDEInternalWorkbenchImages.IMG_OBJS_ERROR_PATH, imageDesc, true);
		configurer.declareImage(IDEInternalWorkbenchImages.IMG_OBJS_WARNING_PATH, imageDesc, true);
		configurer.declareImage(IDEInternalWorkbenchImages.IMG_OBJS_INFO_PATH, imageDesc, true);
	}

	@Override
	public void preStartup() {
		super.preStartup();
		WorkbenchAdapterBuilder.registerAdapters();
	}

	public String getInitialWindowPerspectiveId() {
		return PERSPECTIVE_ID;
	}

}

		
[RCP]org.eclipse.core.resources.IFile to java.io.File
	// IFile から Fileを得る方法
	// IFileからは直接ファイルサイズが取得出来なかったりするので、必要に応じて利用しましょう

IFile file = fileReference;
IFileStore testLocationStore = EFS.getStore(file.getLocationURI());
java.io.File storeAsFile = testLocationStore.toLocalFile(EFS.NONE, null);

	// こんなのもオーケー ってかこっちの方が素敵
	IFile#getLocation()#toFile()
		
[RCP]How to save recently opend files lists
	// 非推奨メソッドを使うことになりますが、これでイケマス。
	// 毎回思うんですけど、RCPってEclipseの本体の行っている必要最低限の作業をなんでしてくれないのかな。


	// save recently opend files lists
	@Override
	public void postWindowClose() {
		XMLMemento memento = XMLMemento.createWriteRoot("hogeeee");
		EditorHistory history = ((Workbench) getWindowConfigurer().getWindow().getWorkbench()).getEditorHistory();
		history.saveState(memento);

		System.out.println(memento);
		PrintWriter out = null;
		try {
			out = new PrintWriter(new FileWriter(RECENTLY_OPEND_FILE));
			memento.save(out);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}

		super.postWindowClose();
	}
	
	// restore recently opend files lists
	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		FileReader reader = null;
		try {
			reader = new FileReader(RECENTLY_OPEND_FILE);
			XMLMemento memento = XMLMemento.createReadRoot(reader);
			EditorHistory history = ((Workbench) getWindowConfigurer().getWindow().getWorkbench()).getEditorHistory();
			history.restoreState(memento);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}


		
[RCP]show/hide ActionSet
		// ツールバーにどのアクションセットを表示するかをカスタマイズする方法
		// 要らないものはhide, 表示したいものはshowです。
		// やはりEclipse3.5以上で、文字ベースでエディタを提供する場合は
		// 矩形選択(四角く選択)出来るActionが備わっているのでデフォルトでツールバーに表示したいですよね。

		IWorkbenchPage page = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow();
		// 非表示
		page.hideActionSet("org.eclipse.ui.edit.text.actionSet.annotationNavigation");
		page.hideActionSet("org.eclipse.ui.edit.text.actionSet.navigation");
		page.hideActionSet("org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo");
		page.hideActionSet("org.eclipse.ui.actionSet.openFiles");
		// 表示
		page.showActionSet("org.eclipse.ui.edit.text.actionSet.presentation");
		
		// Set List
		// 
		// NAME.     AnnotationNavigation
		// COMMAND.  Next Annotation,Previous Annotatin [前後のアノテーションにジャンプ]
		// ID        org.eclipse.ui.edit.text.actionSet.annotationNavigation
		// 
		// NAME.     Convert Line Delemiters
		// COMMAND.  Convert Line Delemiters to [改行コードを変換しまっせ]
		// ID        org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo
		// 
		// NAME.     Editor Presentation
		// COMMAND.  Toggle Block SelectionMode [矩形選択(四角く選択)],Show White Space Charactors [ホワイトスペースを見えるようにしまっせ]
		// ID        org.eclipse.ui.edit.text.actionSet.presentation
		
		
[ZEST]Custom Layout Algorithm [WisteriaLayoutAlgorithm]
		// Wisteria Layout Algorithm
		// 藤の花のようにしだれた感じを出すレイアウトです。
		// ついでにリサイズされたときに自動的に再レイアウトするようにしました。

		import java.util.ArrayList;
		import java.util.HashMap;
		import java.util.List;
		import java.util.Map;
		
		import org.eclipse.swt.SWT;
		import org.eclipse.swt.events.ControlAdapter;
		import org.eclipse.swt.events.ControlEvent;
		import org.eclipse.swt.layout.FillLayout;
		import org.eclipse.swt.widgets.Display;
		import org.eclipse.swt.widgets.Shell;
		import org.eclipse.zest.core.widgets.Graph;
		import org.eclipse.zest.core.widgets.GraphConnection;
		import org.eclipse.zest.core.widgets.GraphNode;
		import org.eclipse.zest.layouts.LayoutEntity;
		import org.eclipse.zest.layouts.algorithms.AbstractLayoutAlgorithm;
		import org.eclipse.zest.layouts.dataStructures.InternalNode;
		import org.eclipse.zest.layouts.dataStructures.InternalRelationship;
		
		public class CustomLayout {
		
			static class WisteriaLayoutAlgorithm extends AbstractLayoutAlgorithm {
				private int totalSteps;
				private int currentStep;
				private int firstNodes;
				private int[] secondNodes;
				private int expandDiv = 120;
		
				public WisteriaLayoutAlgorithm(int styles, int firstNodes, int[] secondNodes) {
					super(styles);
					this.firstNodes = firstNodes;
					this.secondNodes = secondNodes;
				}
		
				private int numOfChildren() {
					return 3;
				}
		
				protected void applyLayoutInternal(InternalNode[] entitiesToLayout,
						InternalRelationship[] relationshipsToConsider, double boundsX,
						double boundsY, double boundsWidth, double boundsHeight) {
					boundsHeight += expandDiv;
					boundsWidth += expandDiv;
		
					totalSteps = entitiesToLayout.length;
					// totalSteps = firstNodes;
					double distanceX = boundsWidth / firstNodes;
					double distanceY = boundsHeight / numOfChildren();
					int xLocation = 0;
					int yLocation = 0;
		
					fireProgressStarted(totalSteps);
		
					for (currentStep = 0; currentStep < firstNodes; currentStep++) {
						LayoutEntity layoutEntity = entitiesToLayout[currentStep]
								.getLayoutEntity();

						layoutEntity.setLocationInLayout(xLocation, yLocation);
						xLocation += distanceX;
						fireProgressEvent(currentStep, totalSteps);
					}
					int maxStep = 0;
					for (int i = 0; i < secondNodes.length; i++) {
						if (maxStep < secondNodes[i]) {
							maxStep = secondNodes[i];
						}
					}
		
					for (int i = 0; i < secondNodes.length; i++) {
						LayoutEntity refEntity = entitiesToLayout[1 + i]
								.getLayoutEntity();
						xLocation = (int) refEntity.getXInLayout();
						distanceY = boundsHeight / (maxStep + 1);
						// distanceY = boundsHeight / (secondNodes[i] + 1);
						yLocation = (int) distanceY;
		
						for (int j = 0; j < secondNodes[i]; currentStep++, j++) {
							LayoutEntity layoutEntity = entitiesToLayout[currentStep]
									.getLayoutEntity();
		
							layoutEntity.setLocationInLayout(xLocation, yLocation);
							yLocation += distanceY;
							fireProgressEvent(currentStep, totalSteps);
						}
					}
					fireProgressEnded(totalSteps);
				}
		
				protected int getCurrentLayoutStep() {
					return 0;
				}
		
				protected int getTotalNumberOfLayoutSteps() {
					return totalSteps;
				}
		
				protected boolean isValidConfiguration(boolean asynchronous,
						boolean continuous) {
					return true;
				}
		
				protected void postLayoutAlgorithm(InternalNode[] entitiesToLayout,
						InternalRelationship[] relationshipsToConsider) {
					// Do nothing
				}
		
				List firstList = new ArrayList();
				Map> secondMap = new HashMap>();
		
				protected void preLayoutAlgorithm(InternalNode[] entitiesToLayout,
						InternalRelationship[] relationshipsToConsider, double x,
						double y, double width, double height) {
				}
		
				public void setLayoutArea(double x, double y, double width,
						double height) {
					// do nothing
				}
			}
		
			public static void main(String[] args) {
				Display d = new Display();
				Shell shell = new Shell(d);
				shell.setText("Custom Layout Example");
				shell.setLayout(new FillLayout());
				shell.setSize(800, 500);
		
				final Graph g = new Graph(shell, SWT.NONE);
		
				GraphNode start = new GraphNode(g, SWT.NONE, "start");
				GraphNode node1 = new GraphNode(g, SWT.NONE, "node1-0");
				GraphNode node2 = new GraphNode(g, SWT.NONE, "node2-0");
				GraphNode node3 = new GraphNode(g, SWT.NONE, "node3-0");
				GraphNode end = new GraphNode(g, SWT.NONE, "end");
				new GraphConnection(g, SWT.NONE, start, node1);
				new GraphConnection(g, SWT.NONE, node1, node2);
				new GraphConnection(g, SWT.NONE, node2, node3);
				new GraphConnection(g, SWT.NONE, node3, end);
		
				GraphNode node1_1 = new GraphNode(g, SWT.NONE, "node1-1");
				GraphNode node1_2 = new GraphNode(g, SWT.NONE, "node1-2");
				GraphNode node1_3 = new GraphNode(g, SWT.NONE, "node1-3");
				new GraphConnection(g, SWT.NONE, node1, node1_1);
				new GraphConnection(g, SWT.NONE, node1_1, node1_2);
				new GraphConnection(g, SWT.NONE, node1_2, node1_3);
		
				GraphNode node2_1 = new GraphNode(g, SWT.NONE, "node2-1");
				GraphNode node2_2 = new GraphNode(g, SWT.NONE, "node2-2");
				GraphNode node3_1 = new GraphNode(g, SWT.NONE, "node3-1");
				new GraphConnection(g, SWT.NONE, node2, node2_1);
				new GraphConnection(g, SWT.NONE, node2_1, node2_2);
				new GraphConnection(g, SWT.NONE, node3, node3_1);
		
				shell.addControlListener(new ControlAdapter() {
					@Override
					public void controlResized(ControlEvent e) {
						if (!g.getLayoutAlgorithm().isRunning()) {
							g.applyLayout();
						}
					}
				});
		
				g.setLayoutAlgorithm(new WisteriaLayoutAlgorithm(SWT.NONE, 5, new int[] { 3,
						2, 1 }), true);
		
				shell.open();
				while (!shell.isDisposed()) {
					while (!d.readAndDispatch()) {
						d.sleep();
					}
				}
			}
		}