nraynaud lol | Code :
- /*
- * Created on 7 juil. 2004
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- package jcoincoin.gui;
- import java.awt.Color;
- import java.awt.Cursor;
- import java.awt.GridLayout;
- import java.awt.Point;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Map;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JScrollPane;
- import javax.swing.JTextPane;
- import javax.swing.SwingUtilities;
- import javax.swing.event.EventListenerList;
- import javax.swing.text.AttributeSet;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.DefaultHighlighter;
- import javax.swing.text.DefaultStyledDocument;
- import javax.swing.text.Element;
- import javax.swing.text.Highlighter;
- import javax.swing.text.SimpleAttributeSet;
- import javax.swing.text.Style;
- import javax.swing.text.StyleConstants;
- import javax.swing.text.StyledDocument;
- import jcoincoin.Clock;
- import jcoincoin.ClockReference;
- import jcoincoin.InputMessage;
- import jcoincoin.Tribune;
- import jcoincoin.TribuneListener;
- import jcoincoin.messages.Decoration;
- import jcoincoin.messages.DecorationHandler;
- import jcoincoin.messages.decoration.BoldDecoration;
- import jcoincoin.messages.decoration.ClockDecoration;
- import jcoincoin.messages.decoration.ItalicDecoration;
- import jcoincoin.messages.decoration.SmileyDecoration;
- import jcoincoin.utils.CircularArray;
- import jcoincoin.utils.ElementTreePanel;
- /**
- *
- *
- * @author nraynaud
- *
- */
- public class NewTribuneGUI {
- private static final String CLOCK_REF = "clockRef";
- private static final String CLOCK = "clock";
- private static final String UA = "user_agent";
- private static final String MOUSE_LISTENER = "mouse_Listener";
- private static final Cursor DefaultCursor = Cursor
- .getPredefinedCursor(Cursor.DEFAULT_CURSOR);
- private static final Cursor HandCursor = Cursor
- .getPredefinedCursor(Cursor.HAND_CURSOR);
- private JFrame frame;
- private JTextPane textpane;
- private CircularArray postData;
- private final Tribune tribune;
- private DecorationTransformer decorator = new DecorationTransformer();
- private final TribuneMouseListener mouseLister = new TribuneMouseListener();
- private final EventListenerList listeners = new EventListenerList();
- /**
- * represents the number of chars written so far in this document, including
- * those that where removed.
- */
- private int absoluteIndex = 0;
- private int absoluteDocumentStart = 0;
- private final Map messageToData = new HashMap();
- private final HighlightManager highlightManager = new HighlightManager();
- /**
- *
- */
- public NewTribuneGUI(final Tribune tribune) {
- super();
- this.tribune = tribune;
- postData = new CircularArray(2000, new CircularArray.RemoveListener() {
- public void removed(Object o) {
- removeMessage((MessagePosition) o);
- }
- });
- tribune.addListener(tribuneListener);
- this.addClockClickListener(new ClockClickListener() {
- public void onClick(NewTribuneGUI gui, Clock clock) {
- System.out.println(clock);
- }
- });
- }
- public JFrame getFrame() {
- frame = new JFrame(tribune.getName());
- frame.getContentPane().setLayout(new GridLayout(1, 2));
- final DefaultStyledDocument document = new DefaultStyledDocument();
- initialiseStyles(document);
- textpane = new JTextPane(document);
- textpane.setEditable(false);
- textpane.addMouseListener(mouseLister);
- textpane.addMouseMotionListener(mouseLister);
- frame.getContentPane().add(new JScrollPane(textpane), 0);
- frame.getContentPane().add(
- new JScrollPane(new ElementTreePanel(textpane)), 1);
- return frame;
- }
- private void appendString(String s, String style) {
- final StyledDocument document = textpane.getStyledDocument();
- appendString(s, document.getStyle(style));
- }
- private void appendString(String s, AttributeSet style) {
- final StyledDocument document = textpane.getStyledDocument();
- try {
- document.insertString(document.getLength(), s, style);
- } catch (BadLocationException e) {
- throw new IllegalStateException("unexpected BadLocationException" );
- }
- }
- private synchronized void appendMessage(final InputMessage message) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- highlightManager.passivateRef();
- final StyledDocument document = textpane.getStyledDocument();
- final int start = document.getLength();
- final SimpleAttributeSet clockSet = new SimpleAttributeSet(
- document.getStyle(CLOCK));
- clockSet.addAttribute(CLOCK, message.getClock());
- clockSet.addAttribute(UA, message.getUserAgent());
- appendString(message.getTimeString(), clockSet);
- appendString(" ", "global" );
- appendString(message.getLogin(), "login" );
- appendString(" ", "global" );
- final int messageStart = document.getLength();
- appendString(message.getText(), "global" );
- decorator.setStart(messageStart);
- message.getMessage().getToplevelDecoration().iterateOn(
- decorator);
- final int postLen = document.getLength() - start;
- MessagePosition entry = new MessagePosition(start, postLen,
- message);
- postData.add(entry);
- messageToData.put(message, entry);
- absoluteIndex += postLen;
- }
- });
- }
- private synchronized void removeMessage(final MessagePosition entry) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- messageToData.remove(entry.message);
- final StyledDocument document = textpane.getStyledDocument();
- try {
- document.remove(0, entry.getLength());
- absoluteDocumentStart += entry.getLength();
- } catch (BadLocationException e) {
- throw new IllegalStateException(
- "unexpected BadLocationException" );
- }
- }
- });
- }
- public void addClockClickListener(ClockClickListener listener) {
- listeners.add(ClockClickListener.class, listener);
- }
- public void removeClockClickListener(ClockClickListener listener) {
- listeners.remove(ClockClickListener.class, listener);
- }
- void fireClockClickEvent(Clock clock) {
- Object[] listeners = this.listeners.getListenerList();
- for (int i = listeners.length - 2; i >= 0; i -= 2) {
- if (listeners[i] == ClockClickListener.class) {
- ((ClockClickListener) listeners[i + 1]).onClick(this, clock);
- }
- }
- }
- private final TribuneListener tribuneListener = new TribuneListener() {
- public void newPostArrived(InputMessage message, Tribune origin) {
- appendMessage(message);
- }
- public void oldPostArrived(InputMessage message, Tribune origin) {
- appendMessage(message);
- }
- };
- private void initialiseStyles(StyledDocument document) {
- Style global = document.addStyle("global", null);
- Style s = document.addStyle(CLOCK, global);
- StyleConstants.setForeground(s, Color.BLUE);
- s = document.addStyle("clockRef", global);
- StyleConstants.setForeground(s, Color.BLUE);
- StyleConstants.setUnderline(s, true);
- s = document.addStyle("bold", global);
- StyleConstants.setBold(s, true);
- s = document.addStyle("italic", global);
- StyleConstants.setItalic(s, true);
- s = document.addStyle("login", global);
- StyleConstants.setForeground(s, Color.RED);
- }
- /**
- * add style to the message starting at the given location according to the
- * structure of the decoration that calls it.
- *
- * @author nraynaud
- *
- */
- private final class DecorationTransformer implements DecorationHandler {
- private int start = 0;
- private AttributeSet styleForDecoration(Decoration deco) {
- final StyledDocument document = textpane.getStyledDocument();
- if (deco instanceof BoldDecoration)
- return document.getStyle("bold" );
- if (deco instanceof ItalicDecoration)
- return document.getStyle("italic" );
- if (deco instanceof ClockDecoration) {
- SimpleAttributeSet set = new SimpleAttributeSet(document
- .getStyle(CLOCK_REF));
- set.addAttribute(CLOCK_REF, ((ClockDecoration) deco)
- .getClockRef());
- set.addAttribute(MOUSE_LISTENER, clockRefListener);
- return set;
- }
- if (deco instanceof SmileyDecoration) {
- SimpleAttributeSet set = new SimpleAttributeSet();
- SmileyDecoration d = (SmileyDecoration) deco;
- try {
- StyleConstants.setIcon(set, new ImageIcon(new URL(d
- .getUrl()), d.getSmiley()));
- } catch (MalformedURLException e) {
- return null;
- }
- return set;
- }
- return null;
- }
- public void endDecoration(Decoration deco) {
- final StyledDocument document = textpane.getStyledDocument();
- AttributeSet set = styleForDecoration(deco);
- if (set != null)
- document.setCharacterAttributes(start, deco.getLength(), set,
- true);
- start -= deco.getStart();
- }
- public void startDecoration(Decoration deco) {
- start += deco.getStart();
- }
- private void setStart(int start) {
- this.start = start;
- }
- }
- /**
- * represents the position of a message in the document of this visual
- * component.
- *
- * the position is always acurate at the moment where you fetch it,
- * whaterver number of messages where deleted in the document since the
- * creation of the position.
- *
- *
- * @author nraynaud
- *
- */
- private final class MessagePosition {
- private final int absoluteStart;
- private final int length;
- private final InputMessage message;
- /**
- * @param absoluteIndex
- * @param lenght
- * @param message
- */
- public MessagePosition(final int start, final int lenght,
- final InputMessage message) {
- assert SwingUtilities.isEventDispatchThread();
- if (start < 0 || lenght < 0)
- throw new IllegalArgumentException(
- "parameters should be positive" );
- this.absoluteStart = start + absoluteDocumentStart;
- this.length = lenght;
- this.message = message;
- }
- /**
- * @return Returns the lenght.
- */
- public int getLength() {
- return length;
- }
- /**
- * @return Returns the message.
- */
- public InputMessage getMessage() {
- return message;
- }
- /**
- * The start index of the post.
- *
- * @return the start index of the post.
- */
- public int getStart() {
- return absoluteStart - absoluteDocumentStart;
- }
- public int getEnd() {
- return getStart() + getLength();
- }
- }
- private final class TribuneMouseListener extends MouseAdapter implements
- MouseMotionListener {
- private final Point point = new Point();
- private Element currentElement = null;
- private MouseElementListener currentElementListener = null;
- private AttributeSet attributeSetForEvent(MouseEvent e) {
- return elementForEvent(e).getAttributes();
- }
- private Element elementForEvent(MouseEvent e) {
- // avoid instantiating a point in this code
- point.x = e.getX();
- point.y = e.getY();
- int pos = textpane.getUI().viewToModel(textpane, point);
- Element element = textpane.getStyledDocument().getCharacterElement(
- pos);
- return element;
- }
- public void mouseClicked(MouseEvent e) {
- AttributeSet set = attributeSetForEvent(e);
- Object o = set.getAttribute(CLOCK);
- if (o != null) {
- fireClockClickEvent((Clock) o);
- }
- }
- /**
- * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
- */
- public void mouseDragged(MouseEvent e) {
- }
- /**
- * manage the current element, and forwards enter/exit events.
- */
- public void mouseMoved(MouseEvent e) {
- assert e.getSource() == textpane;
- Element element = elementForEvent(e);
- if (element == currentElement)
- return;
- if (currentElementListener != null)
- currentElementListener.mouseExited(e, currentElement);
- currentElement = element;
- MouseElementListener listener = (MouseElementListener) element
- .getAttributes().getAttribute(MOUSE_LISTENER);
- currentElementListener = listener;
- if (listener != null)
- listener.mouseEntered(e, element);
- }
- public void mouseExited(MouseEvent e) {
- if (currentElement == null)
- return;
- if (currentElementListener != null)
- currentElementListener.mouseExited(e, currentElement);
- currentElement = null;
- currentElementListener = null;
- }
- }
- /**
- * manages posts highlighting
- *
- *
- * @author nraynaud
- *
- */
- private class HighlightManager {
- private final Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(
- Color.YELLOW);
- private ClockReference currentReference = null;
- private Object currentReferenceTag;
- public void activateRef(ClockReference reference) {
- assert SwingUtilities.isEventDispatchThread();
- if (currentReference != reference) {
- passivateRef();
- currentReference = reference;
- InputMessage message = reference.dereference();
- if (message != null) {
- MessagePosition entry = (MessagePosition) messageToData
- .get(message);
- if (entry != null) {
- Highlighter highlighter = textpane.getHighlighter();
- try {
- currentReferenceTag = highlighter.addHighlight(
- entry.getStart(), entry.getEnd(), painter);
- } catch (BadLocationException e) {
- throw new RuntimeException(e);
- }
- }
- }
- }
- }
- public void passivateRef() {
- assert SwingUtilities.isEventDispatchThread();
- if (currentReference != null) {
- if (currentReferenceTag != null)
- textpane.getHighlighter().removeHighlight(
- currentReferenceTag);
- currentReference = null;
- }
- }
- }
- /**
- * Listeners interested in text element mouse interraction should extend
- * this class.
- *
- * @author nraynaud
- *
- */
- public abstract class MouseElementListener {
- public void mouseEntered(MouseEvent event, Element element) {
- }
- public void mouseExited(MouseEvent event, Element element) {
- }
- public void mouseClicked(MouseEvent event, Element element) {
- }
- }
- private final MouseElementListener clockRefListener = new MouseElementListener() {
- public void mouseEntered(MouseEvent event, Element element) {
- AttributeSet att = element.getAttributes();
- ClockReference ref = (ClockReference) att.getAttribute(CLOCK_REF);
- if (ref != null) {
- textpane.setCursor(HandCursor);
- highlightManager.activateRef(ref);
- }
- }
- public void mouseExited(MouseEvent event, Element element) {
- textpane.setCursor(DefaultCursor);
- highlightManager.passivateRef();
- }
- };
- }
|
tiens, c'est mon code qui compile (chez moi biensûr, je suis le seul à avoir les classes qui vont autour).
quand je marque que c'est à vous de mettre le code qui va bien autour, c'est pas pour rien, il y a une classe et 2 import minimum à mettre.
Sauf qu'on s'en fout de la compilation, je parle de naviguer dans le code source ! |