KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > console > MessageConsolePartition


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.console;
12
13
14 import org.eclipse.jface.text.TypedRegion;
15 import org.eclipse.ui.console.ConsolePlugin;
16 import org.eclipse.ui.console.MessageConsoleStream;
17
18 /**
19  * A partition from a message stream connected to a message console.
20  */

21 public class MessageConsolePartition extends TypedRegion {
22     
23     /**
24      * Associated stream
25      */

26     private MessageConsoleStream fStream;
27     
28     /**
29      * Partition type
30      */

31     public static final String JavaDoc MESSAGE_PARTITION_TYPE = ConsolePlugin.getUniqueIdentifier() + ".MESSAGE_PARTITION_TYPE"; //$NON-NLS-1$
32

33     public MessageConsolePartition(MessageConsoleStream stream, int offset, int length) {
34         super(offset, length, MESSAGE_PARTITION_TYPE);
35         fStream = stream;
36     }
37     
38     /**
39      * @see java.lang.Object#equals(java.lang.Object)
40      */

41     public boolean equals(Object JavaDoc partition) {
42         if (super.equals(partition)) {
43             fStream.equals(((MessageConsolePartition)partition).getStream());
44         }
45         return false;
46     }
47
48     /**
49      * @see java.lang.Object#hashCode()
50      */

51     public int hashCode() {
52         return super.hashCode() + fStream.hashCode();
53     }
54
55     /**
56      * Returns this partition's stream
57      *
58      * @return this partition's stream
59      */

60     public MessageConsoleStream getStream() {
61         return fStream;
62     }
63     
64     /**
65      * Returns whether this partition is allowed to be combined with the
66      * given partition.
67      *
68      * @param partition
69      * @return boolean
70      */

71     public boolean canBeCombinedWith(MessageConsolePartition partition) {
72         int start = getOffset();
73         int end = start + getLength();
74         int otherStart = partition.getOffset();
75         int otherEnd = otherStart + partition.getLength();
76         boolean overlap = (otherStart >= start && otherStart <= end) || (start >= otherStart && start <= otherEnd);
77         return overlap && getType().equals(partition.getType()) && getStream().equals(partition.getStream());
78     }
79     
80     /**
81      * Returns a new partition representing this and the given parition
82      * combined.
83      *
84      * @param partition
85      * @return partition
86      */

87     public MessageConsolePartition combineWith(MessageConsolePartition partition) {
88         int start = getOffset();
89         int end = start + getLength();
90         int otherStart = partition.getOffset();
91         int otherEnd = otherStart + partition.getLength();
92         int theStart = Math.min(start, otherStart);
93         int theEnd = Math.max(end, otherEnd);
94         return createNewPartition(theStart, theEnd - theStart);
95     }
96     
97     /**
98      * Creates a new patition of this type with the given color, offset,
99      * and length.
100      *
101      * @param offset
102      * @param length
103      * @return a new partition with the given range
104      */

105     public MessageConsolePartition createNewPartition(int offset, int length) {
106         return new MessageConsolePartition(getStream(), offset, length);
107     }
108 }
Popular Tags