import java.util.Iterator;
import net.sourceforge.phpdt.tidy.JtidyPlugin;
+
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.texteditor.ITextEditor;
+import org.eclipse.ui.editors.text.TextEditor;
+import org.eclipse.ui.texteditor.ITextEditor;
/**
* @author jan
* @since 13.01.2003
*/
public abstract class AbstractJTidyAction {
- private ITextEditor fTextEditor = null;
- private IStructuredSelection fSelection = null;
-
- /**
- * Parses the given stream with a Tidy Instance, which belongs to this
- * IFile. Problems will be marked on this file.
- * @param in
- * @param file
- * @return InputStream
- */
- protected byte[] parseStreamOfFile(InputStream in, IFile file) {
- deleteTidyMarker(file);
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- JtidyPlugin.getTidyInstance(file).parse(file, in, out);
- return out.toByteArray();
-
- }
-
- /**
- * Deletes all JTidy Marker of this File
- *
- * @param file
- */
- protected void deleteTidyMarker(IFile file) {
- try {
- IMarker[] markers = file.findMarkers(null, false, IResource.DEPTH_ZERO);
- for (int i = 0; i < markers.length; i++) {
- IMarker marker = markers[i];
- if (marker.getAttribute(JtidyPlugin.MARKER_NAME) != null) {
- marker.delete();
- }
- }
- } catch (CoreException e) {
- //LOGGING
- }
- }
-
- /**
- * Parses the Document with Tidy.
- */
- protected void parseDocument(boolean writeBack) throws ParseFailedException {
- IDocument doku = fTextEditor.getDocumentProvider().getDocument(fTextEditor.getEditorInput());
- assertNotNull(doku);
- String content = doku.get();
- IFile file = (IFile) fTextEditor.getEditorInput().getAdapter(IFile.class);
- assertNotNull(file);
- byte[] ret = parseStreamOfFile(new ByteArrayInputStream(content.getBytes()), file);
- if (writeBack) {
- if (ret.length != 0) {
- String cleanedContent = new String(ret);
- doku.set(cleanedContent);
- } else {
- displayError(
- "Formatting skipped",
- "This document has errors that must be fixed before using HTML Tidy to generate a tidied up version.");
- }
-
- }
- }
-
- /**
- * Throws a ParseFailedException, if the given obj is null
- *
- * @param obj
- * @throws ParseFailedException
- */
- protected void assertNotNull(Object obj) throws ParseFailedException {
- if (obj == null) {
- throw new ParseFailedException("A expected 'non-null' Value was null");
- }
- }
-
- /**
- * Updates the enable state of the parent action
- * @param action
- */
- protected void updateParent(IAction action) {
- action.setEnabled(fTextEditor != null || fSelection != null);
- }
-
- /**
- * Parses all Files in the given selection...
- */
- protected void parseSelection(boolean writeBack) {
- Iterator iterator = null;
- iterator = fSelection.iterator();
- while (iterator.hasNext()) {
- // obj => selected object in the view
- Object obj = iterator.next();
-
- // is it a resource
- if (obj instanceof IResource) {
- IResource resource = (IResource) obj;
-
- // check if it's a file resource
- switch (resource.getType()) {
-
- case IResource.FILE :
- // single file:
- IFile file = (IFile) resource;
-
- InputStream in;
- try {
- in = file.getContents();
- byte[] ret = parseStreamOfFile(in, file);
-
- if (writeBack) {
- if (ret.length != 0) {
- InputStream source = new ByteArrayInputStream(ret);
- file.setContents(source, IFile.KEEP_HISTORY, null);
-
- } else {
- displayError(
- "Formatting skipped",
- "This document has errors that must be fixed before using HTML Tidy to generate a tidied up version.");
- }
- }
- } catch (CoreException e) {
- }
- }
- }
- }
- }
- /**
- * Method error.
- * @param string
- * @param string1
- */
- private void error(String lable, String message) {
-
- // TODO:
- }
-
- /**
- * Opens an error dialog to display the given message.
- *
- * @param message the error message to show
- */
- private void displayError(final String lable, final String message) {
- final Shell parentShell = getShell();
- parentShell.getDisplay().syncExec(new Runnable() {
- public void run() {
- MessageDialog.openError(parentShell, lable, message);
- }
- });
- }
- /**
- * Method getShell.
- * @return Shell
- */
- protected abstract Shell getShell();
-
- /**
- * Updates the Selection: if the given selection is of type
- * IStruckturedSelection, fSection is set to this, otehrwise the field is
- * set to null.
- * @param sel
- */
- protected void updateSelection(ISelection sel) {
- if (sel instanceof IStructuredSelection) {
- fSelection = (IStructuredSelection) sel;
- // REVISIT: further determination of types?
- } else {
- fSelection = null;
- }
- }
-
- /**
- * If the given WorkbenchPart is of type ITextEditor, fTextEditor is set to
- * this value, otherwise to null
- *
- */
- protected void updateEditor(IEditorPart part) {
- if (part instanceof ITextEditor) {
- fTextEditor = (ITextEditor) part;
- } else {
- fTextEditor = null;
- }
- }
-
- protected IEditorPart getEditor() {
- return fTextEditor;
- }
+ private ITextEditor fTextEditor = null;
+ private IStructuredSelection fSelection = null;
+
+ /**
+ * Parses the given stream with a Tidy Instance, which belongs to this
+ * IFile. Problems will be marked on this file.
+ * @param in
+ * @param file
+ * @return InputStream
+ */
+ protected byte[] parseStreamOfFile(InputStream in, IFile file) {
+ deleteTidyMarker(file);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ JtidyPlugin.getTidyInstance(file).parse(file, in, out);
+ return out.toByteArray();
+
+ }
+
+ /**
+ * Deletes all JTidy Marker of this File
+ *
+ * @param file
+ */
+ protected void deleteTidyMarker(IFile file) {
+ try {
+ IMarker[] markers = file.findMarkers(null, false, IResource.DEPTH_ZERO);
+ for (int i = 0; i < markers.length; i++) {
+ IMarker marker = markers[i];
+ if (marker.getAttribute(JtidyPlugin.MARKER_NAME) != null) {
+ marker.delete();
+ }
+ }
+ } catch (CoreException e) {
+ //LOGGING
+ }
+ }
+
+ /**
+ * Parses the Document with Tidy.
+ */
+ protected void parseDocument(boolean writeBack) throws ParseFailedException {
+ IDocument doku = fTextEditor.getDocumentProvider().getDocument(fTextEditor.getEditorInput());
+ assertNotNull(doku);
+ String content = doku.get();
+ IFile file = (IFile) fTextEditor.getEditorInput().getAdapter(IFile.class);
+ assertNotNull(file);
+ byte[] ret = parseStreamOfFile(new ByteArrayInputStream(content.getBytes()), file);
+ if (writeBack) {
+ if (ret.length != 0) {
+ String cleanedContent = new String(ret);
+ doku.set(cleanedContent);
+ } else {
+ displayError(
+ "Formatting skipped",
+ "This document has errors that must be fixed before using HTML Tidy to generate a tidied up version.");
+ }
+
+ }
+ }
+
+ /**
+ * Throws a ParseFailedException, if the given obj is null
+ *
+ * @param obj
+ * @throws ParseFailedException
+ */
+ protected void assertNotNull(Object obj) throws ParseFailedException {
+ if (obj == null) {
+ throw new ParseFailedException("A expected 'non-null' Value was null");
+ }
+ }
+
+ /**
+ * Updates the enable state of the parent action
+ * @param action
+ */
+ protected void updateParent(IAction action) {
+ action.setEnabled(fTextEditor != null || fSelection != null);
+ }
+
+ /**
+ * Parses all Files in the given selection...
+ */
+ protected void parseSelection(boolean writeBack) {
+ Iterator iterator = null;
+ iterator = fSelection.iterator();
+ while (iterator.hasNext()) {
+ // obj => selected object in the view
+ Object obj = iterator.next();
+
+ // is it a resource
+ if (obj instanceof IResource) {
+ IResource resource = (IResource) obj;
+
+ // check if it's a file resource
+ switch (resource.getType()) {
+
+ case IResource.FILE :
+ // single file:
+ IFile file = (IFile) resource;
+
+ InputStream in;
+ try {
+ in = file.getContents();
+ byte[] ret = parseStreamOfFile(in, file);
+
+ if (writeBack) {
+ if (ret.length != 0) {
+ InputStream source = new ByteArrayInputStream(ret);
+ file.setContents(source, IFile.KEEP_HISTORY, null);
+
+ } else {
+ displayError(
+ "Formatting skipped",
+ "This document has errors that must be fixed before using HTML Tidy to generate a tidied up version.");
+ }
+ }
+ } catch (CoreException e) {
+ }
+ }
+ }
+ }
+ }
+ /**
+ * Method error.
+ * @param string
+ * @param string1
+ */
+ private void error(String lable, String message) {
+
+ // TODO:
+ }
+
+ /**
+ * Opens an error dialog to display the given message.
+ *
+ * @param message the error message to show
+ */
+ private void displayError(final String title, final String message) {
+ MessageDialog.openError(null, title, message);
+// final Shell parentShell = getShell();
+// parentShell.getDisplay().syncExec(new Runnable() {
+// public void run() {
+// MessageDialog.openError(parentShell, lable, message);
+// }
+// });
+ }
+// /**
+// * Method getShell.
+// * @return Shell
+// */
+// protected abstract Shell getShell();
+
+ /**
+ * Updates the Selection: if the given selection is of type
+ * IStruckturedSelection, fSection is set to this, otehrwise the field is
+ * set to null.
+ * @param sel
+ */
+ protected void updateSelection(ISelection sel) {
+ if (sel instanceof IStructuredSelection) {
+ fSelection = (IStructuredSelection) sel;
+ // REVISIT: further determination of types?
+ } else {
+ fSelection = null;
+ }
+ }
+
+ /**
+ * If the given WorkbenchPart is of type ITextEditor, fTextEditor is set to
+ * this value, otherwise to null
+ *
+ */
+ protected void updateEditor(IEditorPart part) {
+ if (part instanceof ITextEditor) {
+ fTextEditor = (ITextEditor) part;
+ } else {
+ if (part instanceof TextEditor) {
+ fTextEditor = (TextEditor) part;
+ } else {
+ fTextEditor = null;
+ }
+ }
+ }
+
+ protected IEditorPart getEditor() {
+ return fTextEditor;
+ }
}