⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jadeditor.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.ui.internal.editor.jad;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourceAttributes;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.IFormPage;
import org.eclipse.ui.part.EditorPart;

import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.ui.EclipseMEUIStrings;
import eclipseme.ui.internal.utils.ManifestPreferenceStore;

// TODO Error status area... Updated by field editors

/**
 * Editor for specifying the properties of a MIDP 
 * application descriptor.
 * <p />
 * Copyright (c) 2003-2007 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.12 $
 * <br>
 * $Date: 2006/11/12 01:10:49 $
 * <br>
 * @author Craig Setera
 * @see EditorPart
 */
public class JADEditor extends FormEditor 
{
	/**
	 * Get a string value from the resource bundle
	 * @param key
	 * @return
	 */
	private static String getResourceString(String key) {
		return EclipseMEUIStrings.getString(key);
	}

	// The preference store we are using to 
	// represent the edit state	
	private ManifestPreferenceStore preferenceStore;

	// The underlying file being operated on
	private IFile jadFile;
	
	// The most recent timestamp of the JAD file
	private long modificationStamp;

	// Whether to do a clean just after a save
	private boolean cleanRequired;
	
	// The pages in the editor
	private JADRequiredPropertiesEditorPage requiredPropertiesEditorPage;
	private JADMidletsEditorPage midletsEditorPage;
	private JADOptionalPropertiesEditorPage optionalPropertiesEditorPage;
	private JADOTAPropertiesEditorPage otaPropertiesEditorPage;
	private JADUserDefinedPropertiesEditorPage userDefinedPropertiesEditorPage;

	/**
	 * @see org.eclipse.ui.part.MultiPageEditorPart#createPages()
	 */
	protected void addPages() {
		try {
			String title = getResourceString("editor.jad.tab.required_properties");
			requiredPropertiesEditorPage = new JADRequiredPropertiesEditorPage(this);
			addPage(requiredPropertiesEditorPage);
			
			title = getResourceString("editor.jad.tab.midlets");
			midletsEditorPage = new JADMidletsEditorPage(this, title);
			addPage(midletsEditorPage);

			title = getResourceString("editor.jad.tab.optional_properties");
			optionalPropertiesEditorPage = new JADOptionalPropertiesEditorPage(this);
			addPage(optionalPropertiesEditorPage);

			title = getResourceString("editor.jad.tab.ota");
			otaPropertiesEditorPage = new JADOTAPropertiesEditorPage(this);
			addPage(otaPropertiesEditorPage);

			title = getResourceString("editor.jad.tab.user_defined_properties");
			userDefinedPropertiesEditorPage = new JADUserDefinedPropertiesEditorPage(this, title);
			addPage(userDefinedPropertiesEditorPage);
			
		} catch (PartInitException e) {
			EclipseMECorePlugin.log(IStatus.ERROR, e);
		}
	}

	/**
	 * @see org.eclipse.ui.ISaveablePart#isDirty()
	 */
	public boolean isDirty() {
		boolean dirty = false;
		
		Iterator pageIter = pages.iterator();
		while (pageIter.hasNext()) {
			IFormPage formPage = (IFormPage) pageIter.next();

			// If the control hasn't been created yet, it can't be dirty yet
			if ((formPage != null) && (formPage.getPartControl() != null)) {
				if (formPage.isDirty())  {
					dirty = true;
					break;
				}
			}
		}
		
		return dirty;
	}
	
	/**
	 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void doSave(IProgressMonitor monitor) {
		monitor.beginTask("", getPageCount() + 1);
		
		int i = 0;
		Iterator pageIter = pages.iterator();
		while (pageIter.hasNext()) {
			IFormPage formPage = (IFormPage) pageIter.next();

			// If the control hasn't been created yet, it can't be dirty yet
			if ((formPage != null) && (formPage.getPartControl() != null)) {
				if (formPage.isDirty())  {
					formPage.doSave(monitor);
				}
			}
			
			monitor.worked(i + 1);
		}

		try {
			// Attempt to make the file read/write as necessary, using
			// the resource API so that Team providers can get involved.
			if (jadFile.exists() && jadFile.isReadOnly()) {
				ResourceAttributes attributes = jadFile.getResourceAttributes();
				attributes.setReadOnly(false);
				jadFile.setResourceAttributes(attributes);
			}
			
			preferenceStore.save();
			
			if ((jadFile != null) && (jadFile.exists())) {
				jadFile.refreshLocal(IResource.DEPTH_ZERO, monitor);
			}
			
			// Do a clean if requested
			if ((jadFile != null) && cleanRequired) {
				jadFile.getProject().build(IncrementalProjectBuilder.CLEAN_BUILD, monitor);
			}
		} catch (IOException e) {
			EclipseMECorePlugin.log(IStatus.ERROR, e);
		} catch (CoreException e) {
			EclipseMECorePlugin.log(IStatus.ERROR, e);
		}

		monitor.done();
		editorDirtyStateChanged();
	}

	/**
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
	 */
	public void doSaveAs() {
		// Not allowed...
	}

	/**
	 * Get the IPreferenceStore in use for the edit
	 * function.
	 * 
	 * @return
	 */
	public ManifestPreferenceStore getPreferenceStore() {
		return preferenceStore;
	}

	/**
	 * @see org.eclipse.ui.part.EditorPart#gotoMarker(org.eclipse.core.resources.IMarker)
	 */
	public void gotoMarker(IMarker marker) {
		// Nothing to do here...
	}

	/**
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
	 */
	public boolean isSaveAsAllowed() {
		// Don't allow "Save As..."
		return false;
	}

	/**
	 * @see org.eclipse.ui.part.MultiPageEditorPart#setFocus()
	 */
	public void setFocus() {
		File localFile = getLocalFile();
		
		if ((localFile != null) && (localFile.lastModified() > modificationStamp)) {
			if (shouldReloadFile()) {
				updateEditorInput();
				
				requiredPropertiesEditorPage.editorInputChanged();
				optionalPropertiesEditorPage.editorInputChanged();
				otaPropertiesEditorPage.editorInputChanged();
				midletsEditorPage.editorInputChanged();
				userDefinedPropertiesEditorPage.editorInputChanged();
			}
		}

		super.setFocus();
	}

	/**
	 * Return the JAD file (if set)
	 * @return
	 */
	IFile getJadFile() {
		return jadFile;
	}
	
	/**
	 * @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEditorInput)
	 */
	protected void setInput(IEditorInput input) {
		super.setInput(input);
		
		// Update the application descriptor instance
		if (input instanceof IStorageEditorInput) {
			IStorageEditorInput storageInput = (IStorageEditorInput) input;
			try {
				// Read the storage from the file system as 
				// a preference store
				IPath storagePath = storageInput.getStorage().getFullPath();
				if (storagePath != null) {
					IWorkspaceRoot root = EclipseMECorePlugin.getWorkspace().getRoot(); 
					jadFile = root.getFile(storagePath.makeAbsolute());
					
					if ((jadFile != null) && (jadFile.exists())) {
						updateEditorInput();
					}
				}
				
			} catch (Exception e) {
				EclipseMECorePlugin.log(IStatus.WARNING, e);
			}
		} 
	}

	/**
	 * Return a boolean indicating whether the specified property key is 
	 * an allowable user-defined property.
	 * 
	 * @param key
	 * @return
	 */
	boolean isUserDefinedPropertyKey(String key) {
		return
			!requiredPropertiesEditorPage.isManagingProperty(key) &&
			!optionalPropertiesEditorPage.isManagingProperty(key) &&
			!otaPropertiesEditorPage.isManagingProperty(key) &&
			!midletsEditorPage.isManagingProperty(key);
	}

	/**
	 * Set a boolean indicating whether a clean is required when the save
	 * is complete.
	 * 
	 * @param value
	 */
	void setCleanRequired(boolean value) {
		cleanRequired = value;
	}
	
	/**
	 * Return the jad file as a local File instance.
	 * 
	 * @return
	 */
	private File getLocalFile() {
		return jadFile.getLocation().toFile(); 
	}
	
	/**
	 * Return a boolean indicating whether the user wants
	 * to reload the updated file.
	 * 
	 * @return
	 */
	private boolean shouldReloadFile() {
		return MessageDialog.openQuestion(
				getSite().getShell(), 
				"File Updated", 
				"The file has been updated.  Would you like to reload?");
	}

	/**
	 * Update the editor input.
	 * 
	 * @throws IOException
	 */
	private void updateEditorInput() {
		File localFile = getLocalFile();
		modificationStamp = localFile.lastModified();
		String filename = localFile.toString();  
		preferenceStore = new ManifestPreferenceStore(filename);
		
		try {
			preferenceStore.load();
		} catch (IOException e) {
			EclipseMECorePlugin.log(IStatus.WARNING, e);
		}
	}
}

⌨️ 快捷键说明

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