📄 hexview.java
字号:
/*******************************************************************************
* Copyright (c) 2004 Berthold Daum. All rights reserved. This program and the
* accompanying materials are made available under the terms of the Common
* Public License v1.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors: Berthold Daum
******************************************************************************/
package com.bdaum.Hex.views;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.help.WorkbenchHelp;
import org.eclipse.ui.part.ViewPart;
import com.bdaum.Hex.HexHelpConstants;
import com.bdaum.Hex.game.Game;
import com.bdaum.Hex.game.IGame;
public class HexView extends ViewPart implements IStatusListener {
private class ToggleAction extends Action {
public ToggleAction(String title, boolean init) {
super(title, AS_CHECK_BOX);
setChecked(init);
}
}
/* Action IDs */
private static final int WHITE_ACTION = 0;
private static final int BLACK_ACTION = 1;
private static final int BASIC_ACTION = 2;
private static final int ADVANCED_ACTION = 3;
private static final int NEWGAME_ACTION = 4;
private static final int HELP_ACTION = 5;
/* Widgets and Actions */
private Action blackAction, whiteAction, basicAction, advancedAction,
newGameAction, helpAction;
private Canvas canvas;
/* The game engine */
private IGame game;
/**
* Constructs the view content
*/
public void createPartControl(Composite parent) {
canvas = new Canvas(parent, SWT.NO_BACKGROUND);
canvas
.setBackground(parent.getDisplay().getSystemColor(
SWT.COLOR_GRAY));
WorkbenchHelp.setHelp(canvas, HexHelpConstants.HELP_BOARD);
makeActions();
contributeToActionBars();
game = new Game();
game.setStatusListener(this);
game.setDrawingSurface(canvas);
game.newGame();
}
/**
* Create actions
*/
private void makeActions() {
newGameAction = createAction(NEWGAME_ACTION, "New Game",
"Starts new game", HexHelpConstants.HELP_NEWGAME_ACTION);
whiteAction = createToggleAction(WHITE_ACTION, "White",
"Player plays white", HexHelpConstants.HELP_COLOR_ACTION, true);
blackAction = createToggleAction(BLACK_ACTION, "Black",
"Player plays black", HexHelpConstants.HELP_COLOR_ACTION, false);
basicAction = createToggleAction(BASIC_ACTION, "Basic", "Basic Level",
HexHelpConstants.HELP_LEVEL_ACTION, true);
advancedAction = createToggleAction(ADVANCED_ACTION, "Advanced",
"Advanced Level", HexHelpConstants.HELP_LEVEL_ACTION, false);
helpAction = createAction(HELP_ACTION, "Help", "Help for Hex", null);
}
/**
* Create single action
*
* @param id -
* Identification
* @param label -
* Display label
* @param tip -
* Tooltip
* @param helpId -
* ID for context sensitive help
* @return - the created action instance
*/
private Action createAction(final int id, String label, String tip,
String helpId) {
Action action = new Action() {
public void run() {
runAction(id);
}
};
action.setText(label);
action.setToolTipText(tip);
if (helpId != null)
WorkbenchHelp.setHelp(action, helpId);
return action;
}
/**
* Create single toggle action
*
* @param id -
* Identification
* @param label -
* Display label
* @param tip -
* Tooltip
* @param helpId -
* ID for context sensitive help
* @param init -
* initial state
* @return - the created action instance
*/
private Action createToggleAction(final int id, String label, String tip,
String helpId, boolean init) {
Action action = new ToggleAction(label, init) {
public void run() {
runAction(id);
}
};
action.setToolTipText(tip);
if (helpId != null)
WorkbenchHelp.setHelp(action, helpId);
return action;
}
/**
* Run action
*
* @param id -
* Action identification
*/
protected void runAction(int id) {
switch (id) {
case NEWGAME_ACTION:
game.newGame();
break;
case WHITE_ACTION:
game.setPlayerColor(IGame.WHITE);
whiteAction.setChecked(true);
blackAction.setChecked(false);
break;
case BLACK_ACTION:
game.setPlayerColor(IGame.BLACK);
whiteAction.setChecked(false);
blackAction.setChecked(true);
break;
case BASIC_ACTION:
game.setLevel(IGame.LEVEL1);
basicAction.setChecked(true);
advancedAction.setChecked(false);
break;
case ADVANCED_ACTION:
game.setLevel(IGame.LEVEL2);
basicAction.setChecked(false);
advancedAction.setChecked(true);
break;
case HELP_ACTION:
WorkbenchHelp.displayHelp();
break;
}
}
/**
* Construct menu and tool bar
*/
private void contributeToActionBars() {
IActionBars bars = getViewSite().getActionBars();
fillLocalToolBar(bars.getToolBarManager());
}
/**
* Construct tool bar
*
* @param manager -
* the tool bar manager
*/
private void fillLocalToolBar(IToolBarManager manager) {
manager.add(newGameAction);
manager.add(new Separator());
manager.add(whiteAction);
manager.add(blackAction);
manager.add(new Separator());
manager.add(basicAction);
manager.add(advancedAction);
manager.add(new Separator());
manager.add(helpAction);
}
/**
* Pass focus to Canvas-Widget
*/
public void setFocus() {
canvas.setFocus();
}
/**
* Display message in status line
*
* @param message -
* the message to be displayed
*/
public void showMessage(final String message) {
canvas.getDisplay().asyncExec(new Runnable() {
public void run() {
IStatusLineManager sManager = getViewSite().getActionBars()
.getStatusLineManager();
sManager.setMessage(message);
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -