home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / plugin / composer / mapedit / RectArea.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  6.6 KB  |  218 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. package netscape.plugin.composer.mapedit;
  20. import netscape.plugin.composer.io.Tag;
  21. import java.awt.*;
  22. import java.util.*;
  23. import java.net.URL;
  24.  
  25. public class RectArea extends Area {
  26.     static RectArea firstMouseDown(Point pImage) {
  27.         // start out uncompleted
  28.         return new RectArea(new Rectangle(pImage.x,pImage.y,0,0),pImage,"");
  29.     }
  30.  
  31.     private RectArea(Rectangle r,Point pPrev,String u) {
  32.         // Create a completed RectArea.
  33.         super(u);
  34.  
  35.         rectangle = r;
  36.         if (pPrev != null) {
  37.           // copy it.
  38.           prev = new Point(pPrev.x,pPrev.y);
  39.         }
  40.         else {
  41.           prev = null;
  42.         }
  43.     }
  44.  
  45.     public Rectangle boundingBox() {
  46.       // Return a copy.
  47.       return new Rectangle(rectangle.x,rectangle.y,rectangle.width,rectangle.height);
  48.     }
  49.  
  50.     void mouseUp(Point pImage,Rectangle clp) {
  51.       if (completed())
  52.         return;
  53.       update(prev,pImage);
  54.  
  55.       // Complete the RectArea
  56.       prev = null;
  57.     }
  58.  
  59.     void mouseDown(Point pImage,Rectangle clp) {
  60.       if (completed())
  61.         return;
  62.  
  63.       update(prev,pImage);
  64.     }
  65.  
  66.     void mouseMoved(Point pImage,Rectangle clp) {
  67.       if (completed())
  68.         return;
  69.  
  70.       update(prev,pImage);
  71.     }
  72.  
  73.     void mouseDragged(Point pImage,Rectangle clp) {
  74.       if (completed())
  75.         return;
  76.  
  77.       update(prev,pImage);
  78.     }
  79.  
  80.     void clip(Rectangle c) {
  81.       Rectangle r = rectangle;
  82.  
  83.       int right = r.x + r.width;
  84.       int bottom = r.y + r.height;
  85.  
  86.       r.x = Math.max(c.x,r.x);
  87.       r.y = Math.max(c.y,r.y);
  88.       right = Math.min(right,c.x + c.width);
  89.       bottom = Math.min(bottom,c.y + c.height);
  90.       r.width = right - r.x;
  91.       r.height = bottom - r.y;
  92.       if (r.width <= 0 || r.height <= 0) {
  93.         Debug.println("RectArea.clip(): invalid rect");
  94.       }
  95.     }
  96.  
  97.     boolean moveBy(int dx,int dy,Rectangle clp) {
  98.       if (rectangle.x + dx < clp.x) {
  99.         dx = Math.min(clp.x - rectangle.x,0);
  100.       }
  101.       else if (rectangle.x + dx + rectangle.width > clp.x + clp.width) {
  102.         dx = Math.max(clp.x + clp.width - (rectangle.x + rectangle.width),0);
  103.       }
  104.       if (rectangle.y + dy < clp.y) {
  105.         dy = Math.min(clp.y - rectangle.y,0);
  106.       }
  107.       else if (rectangle.y + dy + rectangle.height > clp.y + clp.height) {
  108.         dy = Math.max(clp.y + clp.height - (rectangle.y + rectangle.height),0);
  109.       }
  110.  
  111.  
  112.       if (dx != 0 || dy !=0) {
  113.         rectangle.translate(dx,dy);
  114.         return true;
  115.       }
  116.  
  117.       return false;
  118.     }
  119.  
  120.     boolean resizeBy(int dx,int dy,Rectangle clp) {
  121.       if (rectangle.width + dx > 0 &&
  122.           rectangle.height + dy > 0 &&
  123.           rectangle.x + rectangle.width + dx <= clp.x + clp.width &&
  124.           rectangle.y + rectangle.height + dy <= clp.y + clp.height) {
  125.         rectangle.width += dx;
  126.         rectangle.height += dy;
  127.         return true;
  128.       }
  129.       else {
  130.         return false;
  131.       }
  132.     }
  133.  
  134.     private void update(Point p1,Point p2) {
  135.       rectangle.x = Math.min(p1.x,p2.x);
  136.       rectangle.y = Math.min(p1.y,p2.y);
  137.       rectangle.width = Math.abs(p2.x - p1.x) + 1;
  138.       rectangle.height = Math.abs(p2.y - p1.y) + 1;
  139.     }
  140.  
  141.     boolean forceCompletion() {
  142.         prev = null;
  143.         return true;
  144.     }
  145.  
  146.     boolean completed() {return prev == null;}
  147.  
  148.     boolean containsPoint(int x,int y) {
  149.       return rectangle.inside(x,y);  // jdk 1.1
  150.     }
  151.  
  152.     void draw(Graphics g,Dimension off,boolean selected) {
  153.         // rectangle at the real location in the color, "inner".
  154.         g.setColor(getInner(selected));
  155.         g.drawRect(rectangle.x+off.width,rectangle.y+off.height,
  156.                    rectangle.width,rectangle.height);
  157.         // Two rectangles, one pixel inside and outside, respectively, in the color,
  158.         // "outer".
  159.         g.setColor(getOuter(selected));
  160.         g.drawRect(rectangle.x-1+off.width,rectangle.y-1+off.height,
  161.                   rectangle.width+2,rectangle.height+2);
  162.         g.drawRect(rectangle.x+1+off.width,rectangle.y+1+off.height,
  163.                   rectangle.width-2,rectangle.height-2);
  164.     }
  165.  
  166.     // Return a new Tag with all the information in the RectArea.
  167.     Tag toTag() {
  168.         Tag tag = new Tag("area");
  169.         tag.addAttribute("shape","rect");
  170.         tag.addAttribute("coords",
  171.                 "" + rectangle.x + "," + rectangle.y + "," +
  172.                 (rectangle.x + rectangle.width - 1) + "," +
  173.                 (rectangle.y + rectangle.height - 1));
  174.         String href = getURL().toString();
  175.         if (href.length() > 0) {
  176.           tag.addAttribute("href",href);
  177.         }
  178.         return tag;
  179.     }
  180.  
  181.  
  182.   static RectArea fromTag(Tag tag,URL base) {
  183.     if (tag.getName().equals("AREA") &&
  184. //        tag.containsAttribute("HREF") &&
  185.         tag.containsAttribute("COORDS")) {
  186.  
  187.       // If ANYTHING goes wrong in parsing, we just ignore the tag.
  188.       try {
  189.         String shape = tag.lookupAttribute("SHAPE");
  190.  
  191.         if (shape == null || shape.equalsIgnoreCase("RECT")) {
  192.           String coords = tag.lookupAttribute("COORDS");
  193.           StringTokenizer tok = new StringTokenizer(coords," ,");
  194.           int x1 = Integer.parseInt(tok.nextToken());
  195.           int y1 = Integer.parseInt(tok.nextToken());
  196.           int x2 = Integer.parseInt(tok.nextToken());
  197.           int y2 = Integer.parseInt(tok.nextToken());
  198.           Rectangle rectangle = new Rectangle(Math.min(x1,x2),
  199.                     Math.min(y1,y2),
  200.                     Math.abs(x2 - x1) + 1,
  201.                     Math.abs(y2 - y1) + 1);
  202.           return new RectArea(rectangle,null,get_href(tag,base));
  203.         }
  204.       }
  205.       catch (Exception e) {}
  206.     }
  207.     return null;
  208.   }
  209.  
  210.   /** If non-null, used for creation. */
  211.   private Point prev;
  212.  
  213.   /** The actual size/location of the RectArea. */
  214.   private Rectangle rectangle;
  215. }
  216.  
  217.  
  218.