changeleveldialog.java

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

JAVA
279
字号
/************************************************************
 *
 * 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 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.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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.util.Timer;

public class ChangeLevelDialog extends Dialog 
{
	private TileMap map = null;
	int maxLevel = 0;
	Text txt = null;
	Button ok = null;
	private ButtonsListener buttonsListener = null;
	
	
	public ChangeLevelDialog(Shell shell, TileMap map) 
	{
		super(shell);
		this.map = map;
		maxLevel=SokobanMessages.getInt("MainView.levelset."+map.getPreferences().getMode()+".maxlevel");
		buttonsListener = new ButtonsListener();
	}
	
	protected void configureShell(Shell newShell) 
	{
		super.configureShell(newShell);
		newShell.setText(SokobanMessages.getString("ChangeLevelDialog.title")); //$NON-NLS-1$
	}

	protected Control createDialogArea(Composite parent) 
	{		
		SokobanPlugin.trace(this.getClass().getName(),"Creating ChangeLevelDialog."); //$NON-NLS-1$
	    Composite composite = (Composite)super.createDialogArea(parent);
	    	    
	    GridLayout gridLayout = new GridLayout();
	    gridLayout.numColumns = 3;
	    gridLayout.verticalSpacing = 2;
		gridLayout.horizontalSpacing = 2;
	    gridLayout.makeColumnsEqualWidth = true;
	    composite.setLayout(gridLayout);

		txt = new Text(composite,SWT.BORDER | SWT.RIGHT_TO_LEFT);
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 3;
		txt.setLayoutData(gridData);
		txt.addModifyListener(new ModifyListener()
		{
			public void modifyText(ModifyEvent e)
			{
				try
				{
					Integer aux = new Integer(txt.getText());
					if(aux.intValue() < 1 || aux.intValue() > maxLevel)
						ok.setEnabled(false);
					else
						ok.setEnabled(true);
				}
				catch(NumberFormatException ex)
				{
					ok.setEnabled(false);
				}
			}
		});

	    Button tmp = new Button(composite,SWT.PUSH);
	    tmp.setText("1"); //$NON-NLS-1$
		tmp.setData("1"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		tmp = new Button(composite,SWT.PUSH);
		tmp.setText("2"); //$NON-NLS-1$
		tmp.setData("2"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		tmp = new Button(composite,SWT.PUSH);
		tmp.setText("3"); //$NON-NLS-1$
		tmp.setData("3"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		tmp = new Button(composite,SWT.PUSH);
		tmp.setText("4"); //$NON-NLS-1$
		tmp.setData("4"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		tmp = new Button(composite,SWT.PUSH);
		tmp.setText("5"); //$NON-NLS-1$
		tmp.setData("5"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		tmp = new Button(composite,SWT.PUSH);
		tmp.setText("6"); //$NON-NLS-1$
		tmp.setData("6"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		tmp = new Button(composite,SWT.PUSH);
		tmp.setText("7"); //$NON-NLS-1$
		tmp.setData("7"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		tmp = new Button(composite,SWT.PUSH);
		tmp.setText("8"); //$NON-NLS-1$
		tmp.setData("8"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		tmp = new Button(composite,SWT.PUSH);
		tmp.setText("9"); //$NON-NLS-1$
		tmp.setData("9"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		tmp = new Button(composite,SWT.PUSH);
		tmp.setText(SokobanMessages.getString("ChangeLevelDialog.clear")); //$NON-NLS-1$
		tmp.setData("Clear"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 2;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		tmp = new Button(composite,SWT.PUSH);
		tmp.setText("0"); //$NON-NLS-1$
		tmp.setData("0"); //$NON-NLS-1$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		tmp.setLayoutData(gridData);
		tmp.addMouseListener(buttonsListener);

		Label aux = new Label(composite,SWT.CENTER);
		aux.setText(SokobanMessages.getString("ChangeLevelDialog.fromto") + maxLevel + ")"); //$NON-NLS-1$ //$NON-NLS-2$
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 3;
		aux.setLayoutData(gridData);

	   	return composite;
	}
	
	// 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("ChangeLevelDialog.ok"), true); //$NON-NLS-1$
		ok.setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID, SokobanMessages.getString("ChangeLevelDialog.cancel"), false); //$NON-NLS-1$
	}
	
	protected void okPressed()
	{
		SokobanPlugin.trace(this.getClass().getName(),"Closing ChangeLevelDialog."); //$NON-NLS-1$
		
		map.getPreferences().setLevel(Integer.parseInt(txt.getText()),map.getPreferences().getMode());
		try
		{
			((Timer)map.getCounters().get(Constants.COUNTER_TIME)).resetStopped();
			map.setIsNewLevel(true);
			map.loadMap();
			map.redraw();
			map.getUndoAction().setEnabled(false);

			// Play sound.
			if(map.getPreferences().isSoundEnabled())
				SokobanPlugin.getResourceManager().getSound(Constants.SOUND_START).play();
		}
		catch(IOException ex)
		{
			SokobanPlugin.getDefault().getLog().log(new Status(IStatus.ERROR,SokobanPlugin.getDefault().getBundle().getSymbolicName(),IStatus.ERROR,ex.toString(),null));
		}
		super.okPressed();
	}
	
	class ButtonsListener extends MouseAdapter
	{
		public void mouseUp(MouseEvent e) 
		{
			if(((Button)e.getSource()).getData().toString().equals("Clear")) //$NON-NLS-1$
				txt.setText(""); //$NON-NLS-1$
			else
				txt.append(((Button)e.getSource()).getData().toString());
		}
	}
}

⌨️ 快捷键说明

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