View Javadoc

1   /*
2    * Created on Oct 20, 2005. Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the "License"); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.editor.xmlEditor.actions;
13  import java.util.Map;
14  
15  import org.eclipse.jface.action.Action;
16  import org.eclipse.jface.dialogs.MessageDialog;
17  import org.eclipse.ui.IEditorPart;
18  
19  import sk.uniba.euromath.document.XMLAccess;
20  import sk.uniba.euromath.document.NamespaceManager.NodeCount;
21  import sk.uniba.euromath.editor.MultiViewXMLEditor;
22  import sk.uniba.euromath.editor.actions.MultiViewActionContributor;
23  import sk.uniba.euromath.editor.xmlEditor.actions.lang.Messages;
24  /***
25   * Shows some info about current document - location, namespaces, number of
26   * elements and attributes, etc etc.
27   * @author Martin Vysny
28   */
29  public class ShowDocumentPropertiesAction extends Action {
30  	/***
31  	 * Owner contributor.
32  	 */
33  	private final MultiViewActionContributor contributor;
34  	/***
35  	 * Action constructor.
36  	 * @param contributor owner contributor.
37  	 */
38  	public ShowDocumentPropertiesAction(
39  			final MultiViewActionContributor contributor) {
40  		super(ActionConsts.SHOW_DOCUMENT_PROPERTIES_TEXT);
41  		this.contributor = contributor;
42  		// The id is used to refer to the action in a menu or toolbar
43  		setId(ActionConsts.SHOW_DOCUMENT_PROPERTIES_ACTION);
44  		// Associate the action with a pre-defined command, to allow key
45  		// bindings.
46  		setActionDefinitionId(ActionConsts.SHOW_DOCUMENT_PROPERTIES_ACTION);
47  	}
48  	/*
49  	 * (non-Javadoc)
50  	 * @see org.eclipse.jface.action.Action#run()
51  	 */
52  	@Override
53  	public void run() {
54  		final IEditorPart activeEditor = getContributor().getPage()
55  				.getActiveEditor();
56  		if (!(activeEditor instanceof MultiViewXMLEditor)) {
57                          return;
58                  }
59  		final XMLAccess xml = ((MultiViewXMLEditor)activeEditor).getXMLAccess();
60  		// build a simple dialog text.
61  		final StringBuilder builder = new StringBuilder();
62  		builder.append(Messages.getString("ShowDocumentPropertiesAction.location")).append(xml.getDocumentURL().toString()) //$NON-NLS-1$
63  				.append("\n"); //$NON-NLS-1$
64  		builder.append(Messages.getString("ShowDocumentPropertiesAction.encoding")).append(xml.getEncoding()).append("\n\n"); //$NON-NLS-1$ //$NON-NLS-2$
65  		builder.append(Messages.getString("ShowDocumentPropertiesAction.namespaces")); //$NON-NLS-1$
66  		final Map<String, NodeCount> nodeCount = xml.getNsManager().getCounts();
67  		for (final String ns : nodeCount.keySet()) {
68  			builder.append(Messages.getString("ShowDocumentPropertiesAction.namespace")).append(ns).append("}\n"); //$NON-NLS-1$ //$NON-NLS-2$
69  			builder.append(Messages.getString("ShowDocumentPropertiesAction.prefixMapping")).append( //$NON-NLS-1$
70  					xml.getNsManager().getPrefix(ns)).append("\n"); //$NON-NLS-1$
71  			final NodeCount nc = nodeCount.get(ns);
72  			builder.append(Messages.getString("ShowDocumentPropertiesAction.elements")).append(nc.elements).append( //$NON-NLS-1$
73  					Messages.getString("ShowDocumentPropertiesAction.attributes")).append(nc.attributes).append("\n\n"); //$NON-NLS-1$ //$NON-NLS-2$
74  		}
75  		// show the dialog.
76  		MessageDialog.openInformation(getContributor().getPage()
77  				.getWorkbenchWindow().getShell(), xml.getFileName()
78  				+ Messages.getString("ShowDocumentPropertiesAction.properties"), builder.toString()); //$NON-NLS-1$
79  	}
80      /***
81       * @return Returns the contributor.
82       */
83      protected MultiViewActionContributor getContributor() {
84          return this.contributor;
85      }
86  }