Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER Copyright 2009, 2010 IBM. All rights reserved. Use is subject to license terms. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also obtain a copy of the License at http://odftoolkit.org/docs/license.txt Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 package org.odftoolkit.simple.common.navigation;
 
 
Selection describes one of the matched results, which is recognized by the container element, the start index of the text content in this element and the text content.
 
 public abstract class Selection {
 
 	private OdfElement mElement;
 	private int mIndex;

Get the container element of this Selection.

Returns:
the container element
 
 	public OdfElement getElement() {
 		return ;
 	}

Get the start index of the text content in the container element. This is only meaningful for TextSelection and its sub classes, other type of Selection will return 0.

Returns:
the start index of the container element
 
 	public int getIndex() {
 		return ;
 	}

Cut current Selection.

 
 	public abstract void cut() throws InvalidNavigationException;

Paste current Selection at front of the specified position Selection.

Parameters:
positionItem the position Selection
Throws:
InvalidNavigationException
 
 	public abstract void pasteAtFrontOf(Selection positionItemthrows InvalidNavigationException;

Paste current Selection at end of the specified position Selection.

Parameters:
positionItem the position Selection
Throws:
InvalidNavigationException
 
 	public abstract void pasteAtEndOf(Selection positionItemthrows InvalidNavigationException;

When a selected item has been deleted, the Selections after this deleted Selection should be refreshed, as these Selections index have been changed.

Parameters:
deletedItem the deleted Selection
 
 	protected abstract void refreshAfterFrontalDelete(Selection deletedItem);

When a selected item has been inserted, the Selection after the inserted item should be refresh, as these Selections index have been changed.

Parameters:
insertedItem the inserted Selection
	protected abstract void refreshAfterFrontalInsert(Selection insertedItem);

A quick method to update the index of this Selection.

Parameters:
offset the offset that the index should be added.
	protected abstract void refresh(int offset);

SelectionManager can manage all the Selections that are returned to end users. This SelectionManager contains a repository of all Selections, and will refresh the status/index of Selections after certain operation.
	static class SelectionManager {
Register the Selection item.

Parameters:
item the Selection item
		public static void registerItem(Selection item) {
			OdfElement element = item.getElement();
			if (.containsKey(element)) {
				Vector<Selectionselections = .get(element);
				int i = 0;
				while (i < selections.size()) {
					if (selections.get(i).getIndex() > item.getIndex()) {
						selections.insertElementAt(itemi);
						break;
					}
					i++;
				}
				if (i == selections.size()) {
					selections.add(item);
				}
else {
				Vector<Selectional = new Vector<Selection>();
				al.add(item);
				.put(elemental);
			}
		}

Refresh the Selections in repository after a item is cut.

Parameters:
cutItem the cut item
		public synchronized static void refreshAfterCut(Selection cutItem) {
			// travase the whole sub tree
			OdfElement element = cutItem.getElement();
			if (.containsKey(element)) {
				Vector<Selectionselections = .get(element);
				for (int i = 0; i < selections.size(); i++) {
					if (selections.get(i).getIndex() > cutItem.getIndex()) {
						selections.get(i).refreshAfterFrontalDelete(cutItem);
					}
				}
			}
		}

Refresh the selections in repository after pastedAtFrontOf operation is called.

Parameters:
item the pasted item
positionItem the position item
		public synchronized static void refreshAfterPasteAtFrontOf(Selection itemSelection positionItem) {
			// travase the whole sub tree
			OdfElement element = positionItem.getElement();
			if (.containsKey(element)) {
				Vector<Selectionselections = .get(element);
				for (int i = 0; i < selections.size(); i++) {
					if (selections.get(i).getIndex() >= positionItem.getIndex()) {
						selections.get(i).refreshAfterFrontalInsert(item);
					}
				}
			}
		}

Refresh the Selections in repository after pastedAtEndOf operation is called.

Parameters:
item the pasted item
positionItem the position item
		public synchronized static void refreshAfterPasteAtEndOf(Selection itemSelection positionItem) {
			OdfElement element = positionItem.getElement();
			int positionIndex;
			if (positionItem instanceof TextSelection) {
				positionIndex = positionItem.getIndex() + ((TextSelectionpositionItem).getText().length();
else {
				positionIndex = positionItem.getIndex();
			}
			if (.containsKey(element)) {
				Vector<Selectionselections = .get(element);
				for (int i = 0; i < selections.size(); i++) {
					if (selections.get(i).getIndex() >= positionIndex) {
						selections.get(i).refreshAfterFrontalInsert(item);
					}
				}
			}
		}

Remove the Selection from repository.

Parameters:
item Selection item
		public static void unregisterItem(Selection item) {
			OdfElement element = item.getElement();
			if (.containsKey(element)) {
				Vector<Selectionselections = .get(element);
				selections.remove(item);
			}
		}

A direct method to update all the Selections contained in a element after a certain position.

Parameters:
containerElement the container element
offset the offset
positionIndex the index of a certain position
		public synchronized static void refresh(OdfElement containerElementint offsetint positionIndex) {
			if (.containsKey(containerElement)) {
				Vector<Selectionselections = .get(containerElement);
				for (int i = 0; i < selections.size(); i++) {
					if (selections.get(i).getIndex() >= positionIndex) {
						selections.get(i).refresh(offset);
					}
				}
			}
		}
		private SelectionManager() {
		}
	}
New to GrepCode? Check out our FAQ X