levelcompleteddialog.java

来自「mywork是rcp开发的很好的例子」· Java 代码 · 共 214 行

JAVA
214
字号
/************************************************************
 *
 * 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.io.IOException;
import java.util.ArrayList;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import es.org.chemi.games.sokoban.SokobanMessages;
import es.org.chemi.games.sokoban.SokobanPlugin;
import es.org.chemi.games.sokoban.util.Constants;
import es.org.chemi.games.sokoban.util.LevelInformation;
import es.org.chemi.games.util.Counter;
import es.org.chemi.games.util.Timer;

public class LevelCompletedDialog extends Dialog
{
	private TileMap map = null;
	private int level = 0;
	private int mode = 0;
	private LevelInformation levelInfo = null;
	
	boolean isRecord = false;
	private int newMoves = 0;
	private int newPushes = 0;
	private int newTime = 0;
	
	Text name = null;
	Button ok = null;
	
	public LevelCompletedDialog(Shell shell, TileMap map) 
	{
		super(shell);
		this.map = map;
		
		// Getting current mode.
		mode = map.getPreferences().getMode();

		// Getting current level.		
		level = map.getPreferences().getLevel();
		
		// Getting current level information.
		if(mode == Constants.MODE_ORIGINAL)
			levelInfo = (LevelInformation)((ArrayList)map.getPreferences().getLevelsInfo().get(Constants.INFO_ORIGINAL)).get(map.getPreferences().getLevel()-1);
		else if(map.getPreferences().getMode() == Constants.MODE_EXTRA)
			levelInfo = (LevelInformation)((ArrayList)map.getPreferences().getLevelsInfo().get(Constants.INFO_EXTRA)).get(map.getPreferences().getLevel()-1);
		else
			levelInfo = (LevelInformation)((ArrayList)map.getPreferences().getLevelsInfo().get(Constants.INFO_OTHER)).get(map.getPreferences().getLevel()-1);
	}
	
	protected void configureShell(Shell newShell) 
	{
		super.configureShell(newShell);
		newShell.setText(SokobanMessages.getString("LevelCompletedDialog.title")); //$NON-NLS-1$
	}

	protected Control createDialogArea(Composite parent) 
	{		
		SokobanPlugin.trace(this.getClass().getName(),"Creating LevelCompletedDialog."); //$NON-NLS-1$
	    Composite composite = (Composite)super.createDialogArea(parent);
	    
	    GridLayout gridLayout = new GridLayout();
	    gridLayout.numColumns = 1;
	    gridLayout.marginWidth = 20;
	    gridLayout.horizontalSpacing = 10;
	    composite.setLayout(gridLayout);

		newMoves = (((Counter)map.getCounters().get(Constants.COUNTER_MOVES)).getValue());
		newPushes = (((Counter)map.getCounters().get(Constants.COUNTER_PUSHES)).getValue());
		newTime = (((Timer)map.getCounters().get(Constants.COUNTER_TIME)).getTime());
		checkRecord();

		Label aux = new Label(composite,SWT.CENTER);
		aux.setText(SokobanMessages.getString("LevelCompletedDialog.label1") + map.getPreferences().getLevel() + SokobanMessages.getString("LevelCompletedDialog.label2")); //$NON-NLS-1$ //$NON-NLS-2$
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		aux.setLayoutData(gridData);
		
		if(isRecord)
		{
			aux = new Label(composite,SWT.CENTER);
			aux.setText(SokobanMessages.getString("LevelCompletedDialog.label3")); //$NON-NLS-1$
			gridData = new GridData();
			gridData.horizontalAlignment = GridData.FILL;
			aux.setLayoutData(gridData);
			
			aux = new Label(composite,SWT.CENTER);
			aux.setText(SokobanMessages.getString("LevelCompletedDialog.label4")); //$NON-NLS-1$
			gridData = new GridData();
			gridData.horizontalAlignment = GridData.FILL;
			aux.setLayoutData(gridData);
			
			name = new Text(composite,SWT.CENTER | SWT.BORDER);
			if(map.getPreferences().getLastName() != null){
				name.setText(map.getPreferences().getLastName());
			}
			gridData = new GridData();
			gridData.horizontalAlignment = GridData.FILL;
			name.setLayoutData(gridData);
			name.addModifyListener(new ModifyListener()
			{
				public void modifyText(ModifyEvent e)
				{
					if(name.getText().equals("")) //$NON-NLS-1$
						ok.setEnabled(false);
					else
						ok.setEnabled(true);
				}
			});
		}
    	return composite;
	}

	private void checkRecord() {
		if(map.isDemo)
			return;

		if(!levelInfo.isCompleted())
			isRecord = true;
		else if(levelInfo.isCompleted())
		{
			int oldMoves = levelInfo.getMoves();
			int oldPushes = levelInfo.getPushes();
			int oldTime = levelInfo.getTime();
			if(newMoves < oldMoves)
				isRecord = true;
			else if(newMoves == oldMoves)
			{
				if(newPushes < oldPushes)
					isRecord = true;
				else if(newPushes == oldPushes)
				{
					if(newTime < oldTime)
					{
						isRecord = true;
					}
				}
			}
		}
	}
	
	// Overwrite Dialog method to change the labels of its default buttons.
	protected void createButtonsForButtonBar(Composite parent) 
	{
		// create OK and Cancel buttons by default
		ok = createButton(parent, IDialogConstants.OK_ID, SokobanMessages.getString("LevelCompletedDialog.ok"), true); //$NON-NLS-1$
	}
	
	protected void okPressed()
	{
		SokobanPlugin.trace(this.getClass().getName(),"Closing LevelCompletedDialog."); //$NON-NLS-1$
		if(isRecord && name.getText().equals("")){
			name.setText("default");
		}
		if(isRecord)
		{
			levelInfo.setCompleted(true);
			levelInfo.setPlayer(name.getText());
			map.getPreferences().setLastName(name.getText());
			levelInfo.setMoves(newMoves);
			levelInfo.setPushes(newPushes);
			levelInfo.setTime(newTime);
		}
		if(map.getPreferences().getLevel() == SokobanMessages.getInt("MainView.levelset."+mode+".maxlevel"))
			map.getPreferences().setLevel(1,mode);
		else
			map.getPreferences().setLevel(map.getPreferences().getLevel(mode) + 1,mode);

		try
		{
			((Timer)map.getCounters().get(Constants.COUNTER_TIME)).resetStopped();
			map.setIsNewLevel(true);
			map.loadMap();
			map.redraw();
			map.getUndoAction().setEnabled(false);
			map.hookListener();
			
			// Play sound.
			if(map.getPreferences().isSoundEnabled())
				SokobanPlugin.getResourceManager().getSound(Constants.SOUND_START).play();
			map.getPauseAction().run();
		}
		catch(IOException ex)
		{
			SokobanPlugin.getDefault().getLog().log(new Status(IStatus.ERROR,SokobanPlugin.getDefault().getBundle().getSymbolicName(),IStatus.ERROR,ex.toString(),null));
		}

		super.okPressed();
	}
}

⌨️ 快捷键说明

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