ConflictNavigator.java :  » Source-Control » gruntspud » gruntspud » editor » Java Open Source

Java Open Source » Source Control » gruntspud 
gruntspud » gruntspud » editor » ConflictNavigator.java
/*
 * Gruntspud
 * 
 * Copyright (C) 2002 Brett Smith.
 * 
 * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public License along with this program; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package gruntspud.editor;

import gruntspud.Constants;
import gruntspud.GruntspudContext;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Iterator;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;

/**
 * A component that provides a 'gutter' that represents the entire file and
 * shows where conflicts exist. Click on any position in the gutter will take
 * the user to that part of file.
 * 
 * @author magicthize
 */

public class ConflictNavigator extends JComponent implements AdjustmentListener {

  private JTextComponent text;
  private JScrollPane textScroller;
  private List conflicts;
  private Color local, remote, separator;

  /**
   * Construct a new ConflictNavigator
   * 
   * @param text text component
   * @param textScroller scroller
   * @param conflicts list of conflicts
   * @param context context
   */
  public ConflictNavigator(JTextComponent text, JScrollPane textScroller, List conflicts, GruntspudContext context) {

    this.text = text;
    this.textScroller = textScroller;
    this.conflicts = conflicts;

    local = context.getTextStyleModel().getStyle(Constants.OPTIONS_STYLE_CONFLICT_LOCAL).getBackground();
    remote = context.getTextStyleModel().getStyle(Constants.OPTIONS_STYLE_CONFLICT_REMOTE).getBackground();
    separator = context.getTextStyleModel().getStyle(Constants.OPTIONS_STYLE_CONFLICT_SEPARATOR).getBackground();

    setBackground(text.getBackground());
    setForeground(text.getForeground());
    textScroller.getVerticalScrollBar().addAdjustmentListener(this);
    addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent evt) {
        scrollTo(evt.getY());

      }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent evt) {
        scrollTo(evt.getY());

      }
    });
  }

  private void scrollTo(int scrollTo) {
    Insets margin = getBorder() == null ? new Insets(0, 0, 0, 0) : getBorder().getBorderInsets(this);
    Dimension size = new Dimension(getSize().width - margin.left - margin.right, getSize().height - margin.top - margin.bottom);
    float scale = (float) size.height / (float) text.getSize().height;
    Rectangle r = textScroller.getViewport().getViewRect();
    textScroller
        .getViewport()
        .setViewPosition(new Point(0, Math.min(Math.max(0, (int) ((float) scrollTo / scale) - (r.height / 2)),
            (text.getSize().height - r.height))));
  }

  public Dimension getMinimumSize() {
    return new Dimension(12, super.getMinimumSize().height);
  }

  public Dimension getMaximumSize() {
    return new Dimension(12, super.getMaximumSize().height);
  }

  public Dimension getPreferredSize() {
    return new Dimension(12, super.getPreferredSize().height);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(getBackground());
    Insets margin = getBorder() == null ? new Insets(0, 0, 0, 0) : getBorder().getBorderInsets(this);
    FontMetrics metrics = text.getFontMetrics(text.getFont());
    Dimension size = new Dimension(getSize().width - margin.left - margin.right, getSize().height - margin.top - margin.bottom);
    g.fillRect(0, 0, size.width, size.height);
    int len = text.getDocument().getLength();
    len = len == 0 ? 0 : len - 1;
    try {
      Rectangle r = text.modelToView(len);
      float lastLine = (r.y + r.height) / metrics.getHeight();
      float scale = (float) size.height / lastLine;
      for (Iterator i = conflicts.iterator(); i.hasNext(); ) {
        Conflict c = (Conflict) i.next();

        //  Local
        r = text.modelToView(c.getStart1());
        float startLocal = r.y / metrics.getHeight();
        int localY = (int) (startLocal * scale);

        //  Separator
        r = text.modelToView(c.getStart1() + c.getLength1());
        float startSeparator = r.y / metrics.getHeight();
        int separatorY = (int) (startSeparator * scale);

        //  Remote
        r = text.modelToView(c.getStart2());
        float startRemote = r.y / metrics.getHeight();
        int remoteY = (int) (startRemote * scale);

        // End
        r = text.modelToView(c.getStart2() + c.getLength2());
        float end = (r.y + r.height) / metrics.getHeight();
        int endY = (int) (end * scale);

        g.setColor(local);
        g.fillRect(margin.left, margin.top + localY, size.width - margin.left - margin.right, separatorY - localY);
        g.setColor(separator);
        g.fillRect(margin.left, margin.top + separatorY, size.width - margin.left - margin.right, remoteY - separatorY);
        g.setColor(remote);
        g.fillRect(margin.left, margin.top + remoteY, size.width - margin.left - margin.right, endY - remoteY);

      }
      Color c = text.getForeground();
      c = new Color(c.getRed(), c.getGreen(), c.getBlue(), 128);
      g.setColor(c);
      r = textScroller.getViewport().getViewRect();
      scale = (float) size.height / (float) text.getSize().height;
      int y = (int) ((r.y + (r.height / 2)) * scale);
      g.drawRect(margin.left, margin.top + y - 1, size.width - margin.left - margin.right - 1, 3);
    }
    catch (BadLocationException e) {
      Constants.UI_LOG.error(e);
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event.AdjustmentEvent)
   */
  public void adjustmentValueChanged(AdjustmentEvent e) {
    repaint();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.