Professional Documents
Culture Documents
}
if (queue[priority] == null) {
queue[priority] = new LinkedList();
}
queue[priority].add(element);
modCount++;
}
The modCount variable is inherited from AbstractList (more about
this variable later).
The fetching operations are next: getFirst and get. The getFirst
method returns the highest priority element. The get method
returns the element at a specific index. Any Collection
implementation needs an iterator(), so you can rely on its
existence to simplify the fetching methods. For getFirst, just
return the first element of the iterator. For get you need to
count.
public Object getFirst() {
return iterator().next();
}
public Object get(int index) {
if (index < 0) {
throw new IllegalArgumentException(
"Illegal index: "+ index);
}
Iterator iter = iterator();
int pos = 0;
while (iter.hasNext()) {
if (pos == index) {
return iter.next();
} else
{
pos++;
}
}
return null;
}
Removal works in a similar way to fetching, in that it relies on
the iterator where possible. There are two removal methods to
implement: clear and removeFirst. With clear, you need to clear
the elements for each priority in the internal array. With
removeFirst, you remove and return the first element of the
iterator, the highest priority item.
public void clear() {
for (int i=0, n=queue.length; i<n; i++) {
queue[i].clear();
}
}
public Object removeFirst() {
Iterator iter = iterator();
Object obj = iter.next();
iter.remove();
return obj;
}
The bulk of the code is the iterator, which is shown below with
the full class definition. The purpose of an Iterator is to visit
each item in the Collection. For the PriorityQueue iterator, it
must not only visit each item, but visit each item in priority
order. The way this iterator does its work is by starting with
the iterator for the highest priority list, and visiting all of
those items. When the end of that iterator is hit, the iterator
for the next priority item comes into play, and so on until no
more priorities (and their associated iterator) is available.
The previously mentioned modCount variable is used to check for
modifications within the queue while any iterator is being
traversed. This iterator also supports the removal of elements
from the queue. This support is an implementation of the optional
remove method and it is utilized by the removeFirst method. The
remainder of the List interface methods are inherited from
AbstractList.
Here is the code for the priority queue:
import java.io.Serializable;
import java.util.*;
public class PriorityQueue
extends AbstractList
implements Serializable {
private final static int DEFAULT_PRIORITY_COUNT = 10;
private final static int DEFAULT_PRIORITY = 0;
private List queue[];
public PriorityQueue() {
this(DEFAULT_PRIORITY_COUNT);
}
public PriorityQueue(Collection col) {
this(col, DEFAULT_PRIORITY_COUNT);
}
public PriorityQueue(int count) {
this(null, count);
}
public PriorityQueue(Collection col, int count) {
if (count <= 0) {
throw new IllegalArgumentException(
"Illegal priority count: "+ count);
}
queue = new List[count];
if (col != null) {
addAll(col);
}
}
public boolean add(Object element) {
insert(element, DEFAULT_PRIORITY);
return true;
}
lastRet = -1;
expectedModCount = modCount;
}
};
return iter;
}
public String toString() {
StringBuffer buffer = new StringBuffer("{");
for (int n=queue.length-1, i=n; i>=0; --i) {
if (i != n) {
buffer.append(",");
}
buffer.append(i + ":");
if ((queue[i] != null) && (
queue[i].size() > 0)) {
buffer.append(queue[i].toString());
}
}
buffer.append("}");
return buffer.toString();
}
}
The following program tests the priority queue and some of its
capabilities. First, the test program creates a PriorityQueue
object from elements passed in the command line. Then, the
program creates an empty queue and tries to remove an element,
receiving an expected exception. Finally, the program fills the
queue with some names, and then removes each element in priority
order.
import java.util.*;
public class TestPriorityQueue {
public static void main (String args[]) {
List list = Arrays.asList(args);
PriorityQueue queue = new PriorityQueue(list);
System.out.println(queue);
queue = new PriorityQueue(10);
try {
System.out.println(queue.removeFirst());
} catch (NoSuchElementException e) {
System.out.println(
"Received expected exception");
}
queue.insert("Joy", 8);
queue.insert("Scott", 9);
queue.insert("Sueltz", 5);
queue.insert("Bill", 8);
queue.insert("McNealy", 9);
queue.insert("Patricia", 5);
queue.insert("C.", 5);
queue.insert("Papadopoulos", 4);
queue.insert("Greg", 4);
System.out.println(queue);
queue.addAll(list);
System.out.println(queue);
while (queue.size() != 0) {
System.out.println(queue.removeFirst());
}
}
}
Run the test program as follows from the command line:
java TestPriorityQueue one two three four
You should see the following output:
{9:,8:,7:,6:,5:,4:,3:,2:,1:,0:[one, two, three, four]}
Received expected Exception
{9:[Scott, McNealy],8:[Joy, Bill],7:,6:,
5:[Sueltz, Patricia, C.],4:[Papadopoulos, Greg],3:,2:,1:,0:}
{9:[Scott, McNealy],8:[Joy, Bill],7:,6:,
5:[Sueltz, Patricia, C.],4:[Papadopoulos, Greg],3:,2:,1:,
0:[one, two, three, four]}
Scott
McNealy
Joy
Bill
Sueltz
Patricia
C.
Papadopoulos
Greg
one
two
three
four
For more information about creating a priority queue, see
Chapter 9, "Lists", in "Java Collections" by John Zukowski
(http://java.sun.com/jdc/Books/javaprogramming/JavaCollections/).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Displaying Text In Multiple Styles
A commonly asked question is how to display text in multiple
colors or styles within a JTextArea component. The short answer
is: you can't. Both the JTextField and JTextArea components are
designed to display text in a single style, color, font. When you
change the characteristics of the text, you are changing all
text in the component. You can't change the characteristics of
only the selected text, or just the piece you want changed.
Just because the JTextArea component can't display its content in
multiple colors or styles, doesn't mean there isn't support for
displaying text in multiple styles. It just isn't done with the
JTextArea. You need to use the JTextPane. Knowing that it is the
JTextPane and not the JTextArea will lead you to the actual "how"
answer to the original question.
Within the JTextPane, text that you add to the component is
associated with an AttributeSet. The AttributeSet contains a set
of key-value pairs for the various display attributes of the text.
These pairs can answer questions like "what's the current font?",
"what's the background color?", and "with what alignment should
I display the current paragraph?" By setting this AttributeSet
before adding the text, you can change the way the added text
CharacterConstants
ColorConstants
FontConstants
ParagraphConstants
To change the set of attributes for newly-added text, you add the
necessary attribute to an AttributeSet, and associate the set
with the JTextPane. You can also replace the existing set, or as
previously mentioned, change the attributes for the current text
selection.
SimpleAttributeSet set = new SimpleAttributeSet();
set.addAttribute(
StyleConstants.CharacterConstants.Bold,
Boolean.TRUE);
JTextPane tp = new JTextPane();
// false = don't replace attribute for all text
tp.setCharacterAttributes(set, false);
In addition to using addAttribute to add attributes to the set,
there are some helper methods in the StyleConstants class. For
instance, instead of having to use
StyleConstants.CharacterConstants.Bold, as
shown above, you can simply call the setBold method of
StyleConstants, and pass in the appropriate AttributeSet and
boolean value:
StyleConstants.setBold(set, true);
Actually, the first argument to the StyleConstants methods must
be a MutableAttributeSet, which is an extension of the
AttributeSet interface. SimpleAttributeSet implements both
MutableAttributeSet and AttributeSet. See the StyleConstants
class definition for the full set of methods available
(http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/text/
StyleConstants.CharacterConstants.html).
Because the setText method of JTextPane will replace all the
content, a better way to add multi-attributed text is to work
with the component's Document, which contains the text and its
attributes. Get the Document with getStyledDocument, and add text
to it with insertString. The insertString method requires as
arguments, the position to add, the text to add, and the attribute
set. The method can also throw a BadLocationException, which you
must catch or pass along.
Document doc = pane.getStyledDocument();
try {
doc.insertString(doc.getLength(), "Blind", set);
} catch (BadLocationException e) {
System.err.println("Bad location");
return;
}
To demonstrate, let's provide a JTextPane with three words, where
each word has a different style. Notice the different ways the
attribute sets are initialized and associated with the JTextPane.
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class Multi {
public static void main(String args[]) {
JFrame frame = new JFrame(
"Multiattributed text");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
Container content = frame.getContentPane();
JTextPane pane = new JTextPane();
SimpleAttributeSet set =
new SimpleAttributeSet();
set.addAttribute(
StyleConstants.CharacterConstants.Bold,
Boolean.TRUE);
// Initialize attributes before adding text
pane.setCharacterAttributes(set, true);
pane.setText("Three");
set = new SimpleAttributeSet();
StyleConstants.setItalic(set, true);
StyleConstants.setForeground(set, Color.PINK);
StyleConstants.setBackground(set, Color.GREEN);
Document doc = pane.getStyledDocument();
try {
doc.insertString(
doc.getLength(), "Blind", set);
} catch (BadLocationException e) {
System.err.println("Bad location");
return;
}
set = new SimpleAttributeSet();
StyleConstants.setFontSize(set, 48);
try {
doc.insertString(
doc.getLength(), "Mice", set);
} catch (BadLocationException e) {
System.err.println("Bad location");
return;
}
JScrollPane scrollPane = new JScrollPane(pane);
content.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.show();
}
}
For more information about attribute sets and style constants,
see Chapter 23 "Customizing Text Components" in "Graphic Java,
Mastering the JFC, 3rd Edition, Volume 2: Swing" by David Geary
(http://www.sun.com/books/catalog/swing/index.html).
. . . . . . . . . . . . . . . . . . . . . . .
IMPORTANT: Please read our Terms of Use, Privacy, and Licensing
policies:
http://www.sun.com/share/text/termsofuse.html
http://www.sun.com/privacy/
http://developer.java.sun.com/berkeley_license.html
* FEEDBACK