mainview.java

来自「mywork是rcp开发的很好的例子」· Java 代码 · 共 459 行 · 第 1/2 页

JAVA
459
字号
/************************************************************
 *
 * Copyright (c) 2003 Chemi. All rights reserved.
 * 
 * This program and the accompanying materials
 * are made available under the terms of the MIT License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/mit-license.html
 *
 ************************************************************/

package es.org.chemi.games.sokoban.ui;

import java.applet.Applet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.part.ViewPart;

import es.org.chemi.games.sokoban.CounterStatus;
import es.org.chemi.games.sokoban.SokobanMessages;
import es.org.chemi.games.sokoban.SokobanPlugin;
import es.org.chemi.games.sokoban.actions.AboutAction;
import es.org.chemi.games.sokoban.actions.BackgroundAction;
import es.org.chemi.games.sokoban.actions.ChangeLevelAction;
import es.org.chemi.games.sokoban.actions.CheckLevelsAction;
import es.org.chemi.games.sokoban.actions.DemoAction;
import es.org.chemi.games.sokoban.actions.ExitAction;
import es.org.chemi.games.sokoban.actions.HelpAction;
import es.org.chemi.games.sokoban.actions.KeyAction;
import es.org.chemi.games.sokoban.actions.LevelsetAction;
import es.org.chemi.games.sokoban.actions.NextLevelAction;
import es.org.chemi.games.sokoban.actions.PauseAction;
import es.org.chemi.games.sokoban.actions.SkinAction;
import es.org.chemi.games.sokoban.actions.SoundAction;
import es.org.chemi.games.sokoban.actions.UndoAction;
import es.org.chemi.games.sokoban.util.Constants;
import es.org.chemi.games.sokoban.util.LevelInformation;
import es.org.chemi.games.sokoban.util.Preferences;

public class MainView extends ViewPart
{
	Preferences preferences = null;
	TileMap map = null;
	HashMap counters = null;
	
	private PauseAction pauseAction = null;
	private UndoAction undoAction = null;
	private CounterStatus counterStatus;
	
	public void setFocus() 
	{
		map.setFocus();
	}
	
	public void init(IViewSite site, IMemento memento)
	{
		try
		{
			init(site);
		}
		catch(PartInitException ex)
		{
			SokobanPlugin.getDefault().getLog().log(new Status(IStatus.ERROR,SokobanPlugin.getDefault().getBundle().getSymbolicName(),IStatus.ERROR,ex.toString(),null));
		}
		// Restore state.
		restoreState(SokobanPlugin.getDefault().getPreferenceStore());

		// Loading images.
		loadImages();
		
		// Loading sounds.
		loadSounds();
	}

	private void loadImages() 
	{
		// Loading images
		SokobanPlugin.trace(this.getClass().getName(),"Loading images."); //$NON-NLS-1$
		
			
		try{
			reloadSkinImage();
			reloadBackgound();
		}			
		catch(Exception ex){
			SokobanPlugin.getDefault().getLog().log(new Status(IStatus.ERROR,SokobanPlugin.getDefault().getBundle().getSymbolicName(),IStatus.ERROR,ex.toString(),null));			
		}
	}

	public void reloadBackgound() throws IOException {
		String s="icons/background/"+preferences.getBackgroundMode()+".jpg";
		if(SokobanPlugin.getResourceManager().getImage(s) != null)
			SokobanPlugin.getResourceManager().getImage(s).dispose();
		SokobanPlugin.getResourceManager().putImage(Constants.IMAGE_BACKGROUND,new Image(Display.getCurrent(),SokobanPlugin.getResource(s).openStream()));
	}

	public void reloadSkinImage() throws IOException {
		String[] imageNames=new String[]{
				Constants.IMAGE_BOY_UP_TRANSPARENT,
				Constants.IMAGE_BOY_DOWN_TRANSPARENT,
				Constants.IMAGE_BOY_RIGHT_TRANSPARENT,
				Constants.IMAGE_BOY_LEFT_TRANSPARENT,
				Constants.IMAGE_BOX,
				Constants.IMAGE_BOX_OK,
				Constants.IMAGE_WALL,		
				Constants.IMAGE_HOLE,
				Constants.IMAGE_BOY_UP,
				Constants.IMAGE_BOY_DOWN,
				Constants.IMAGE_BOY_RIGHT,
				Constants.IMAGE_BOY_LEFT,
				Constants.IMAGE_FLOOR,
				Constants.IMAGE_TRACK
		};
		//先清场,再载入
		for(String s:imageNames){
			if(SokobanPlugin.getResourceManager().getImage(s) != null)
				SokobanPlugin.getResourceManager().getImage(s).dispose();
			loadSkinImage(s,preferences.getSkinMode());
		}
		//载入16类墙
		for(int i=0;i<Constants.WALL_TYPE;i++){
			String s=Constants.IMAGE_WALL.replace(".", "_"+i+".");
			if(SokobanPlugin.getResourceManager().getImage(s) != null)
				SokobanPlugin.getResourceManager().getImage(s).dispose();
			loadSkinImage(s,preferences.getSkinMode());
		}
		//载入16类外地板
		for(int i=0;i<Constants.WALL_TYPE;i++){
			String s=Constants.IMAGE_FLOOR.replace(".", "_"+i+".");
			if(SokobanPlugin.getResourceManager().getImage(s) != null)
				SokobanPlugin.getResourceManager().getImage(s).dispose();
			loadSkinImage(s,preferences.getSkinMode());
		}
	}

	private void loadSkinImage(String ima,int skin) throws IOException {
		SokobanPlugin.getResourceManager().putImage(ima,new Image(Display.getCurrent(),SokobanPlugin.getResource("icons/"+ skin+"/"+ ima).openStream()));
	}
	
	private void loadSounds()
	{
		// Loading sounds
		SokobanPlugin.trace(this.getClass().getName(),"Loading sounds."); //$NON-NLS-1$

		SokobanPlugin.getResourceManager().putSound(Constants.SOUND_START, Applet.newAudioClip(SokobanPlugin.getResource("sounds/" + Constants.SOUND_START))); //$NON-NLS-1$
		SokobanPlugin.getResourceManager().putSound(Constants.SOUND_FINISH, Applet.newAudioClip(SokobanPlugin.getResource("sounds/" + Constants.SOUND_FINISH))); //$NON-NLS-1$
		SokobanPlugin.getResourceManager().putSound(Constants.SOUND_BACK, Applet.newAudioClip(SokobanPlugin.getResource("sounds/" + Constants.SOUND_BACK))); //$NON-NLS-1$
		SokobanPlugin.getResourceManager().putSound(Constants.SOUND_MOVE, Applet.newAudioClip(SokobanPlugin.getResource("sounds/" + Constants.SOUND_MOVE))); //$NON-NLS-1$
		SokobanPlugin.getResourceManager().putSound(Constants.SOUND_PUSH, Applet.newAudioClip(SokobanPlugin.getResource("sounds/" + Constants.SOUND_PUSH))); //$NON-NLS-1$
	}

	public void createPartControl(Composite parent) 
	{
		SokobanPlugin.trace(this.getClass().getName(),"Creation of Minesweeper main view started."); //$NON-NLS-1$
		// Create UI.
		open(parent);
		
		SokobanPlugin.trace(this.getClass().getName(),"Creation of Minesweeper main view finished."); //$NON-NLS-1$
	}
	
	private void restoreState(IPreferenceStore store)
	{
		// Restore State.
		SokobanPlugin.trace(this.getClass().getName(),"Restoring persistent state."); //$NON-NLS-1$
				
		preferences = new Preferences();
		
		// Restore mode.
		preferences.setMode(store.getInt("mode")); //$NON-NLS-1$
		
		// Restore level.
		preferences.setLevel(store.getInt(Constants.LEVEL_ORIGINAL),Constants.MODE_ORIGINAL);
		preferences.setLevel(store.getInt(Constants.LEVEL_EXTRA),Constants.MODE_EXTRA);
		preferences.setLevel(store.getInt(Constants.LEVEL_OTHER),Constants.MODE_OTHER);
		if(preferences.getMode() == Constants.MODE_ORIGINAL)
			preferences.setLevel(preferences.getLevel(Constants.MODE_ORIGINAL));
		else if(preferences.getMode() == Constants.MODE_EXTRA)
			preferences.setLevel(preferences.getLevel(Constants.MODE_EXTRA));
		else
			preferences.setLevel(preferences.getLevel(Constants.MODE_OTHER));			
		
		// Restore level information
		HashMap levelsInfo = new HashMap();
		StringTokenizer st = new StringTokenizer(store.getString(Constants.INFO_ORIGINAL),"#");
		ArrayList tmp = new ArrayList();
		while(st.hasMoreTokens())
			tmp.add(parse(st.nextToken()));
		levelsInfo.put(Constants.INFO_ORIGINAL,tmp);
		
		st = new StringTokenizer(store.getString(Constants.INFO_EXTRA),"#");
		tmp = new ArrayList();
		while(st.hasMoreTokens())
			tmp.add(parse(st.nextToken()));
		levelsInfo.put(Constants.INFO_EXTRA,tmp);
			
		st = new StringTokenizer(store.getString(Constants.INFO_OTHER),"#");
		tmp = new ArrayList();
		while(st.hasMoreTokens())
			tmp.add(parse(st.nextToken()));
		levelsInfo.put(Constants.INFO_OTHER,tmp);
		
		preferences.setLevelsInfo(levelsInfo);

		// Restore sound mode.
		preferences.setSoundEnabled(store.getBoolean(Constants.SOUND_MODE));
		preferences.setSkinMode(store.getInt(Constants.SKIN_MODE));
		preferences.setKey3DEnabled(store.getBoolean(Constants.KEY_MODE));
		preferences.setBackgroundMode(store.getInt(Constants.BACKGROUND_MODE));

⌨️ 快捷键说明

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