1|package com.infocom.print;

  2|

  3|

  4|/**

  5| * Class: PFPrintObject <p>

  6| *

  7| * @author Jean-Pierre Dube <jpdube@videotron.ca>

  8| * @version 1.0

  9| * @since 1.0

 10| */

 11|

 12|

 13|import java.util.*;

 14|import java.awt.*;

 15|import java.awt.print.PageFormat;

 16|

 17|

 18|public abstract class PFPrintObject {

 19|

 20|

 21|   //--- Public constants declarations

 22|   public static final int STICKY_LEFT = 0;

 23|   public static final int STICKY_RIGHT = 1;

 24|   public static final int STICKY_CENTER = 2;

 25|   public static final int STICKY_TOP = 3;

 26|   public static final int STICKY_BOTTOM = 4;

 27|   public static final int STICKY_NONE = 5;

 28|

 29|

 30|   //--- Private instances declarations

 31|   private PFPoint position = new PFPoint (new PFInchUnit (0.0), new PFInchUnit (0.0));

 32|   private PFSize size = new PFSize (new PFInchUnit (1.0), new PFInchUnit (1.0));

 33|

 34|   private Vector printObjectCollection = new Vector ();

 35|

 36|   private PFPrintObject parentObject = null;

 37|   private PFPage page = null;

 38|

 39|   private int verticalSticky = STICKY_NONE;

 40|   private int horizontalSticky = STICKY_NONE;

 41|   private boolean horizontalFill = false;

 42|   private boolean verticalFill = false;

 43|

 44|   private PFPoint drawOrigin = new PFPoint (new PFInchUnit (0.0), new PFInchUnit (0.0));

 45|   private PFSize drawSize = new PFSize (new PFInchUnit (0.0), new PFInchUnit (0.0));

 46|

 47|   private boolean relativeMode = true;

 48|

 49|   private PFUnit leftMargin = new PFInchUnit (0.0);

 50|   private PFUnit rightMargin = new PFInchUnit (0.0);

 51|   private PFUnit topMargin = new PFInchUnit (0.0);

 52|   private PFUnit bottomMargin = new PFInchUnit (0.0);

 53|

 54|

 55|   /**

 56|    * Constructor: PFPrintObject <p>

 57|    *

 58|    */

 59|   public PFPrintObject () {

 60|

 61|   }

 62|

 63|

 64|   /**

 65|    * Method: print <p>

 66|    *

 67|    * @param parG a value of type Graphics2D

 68|    */

 69|   public abstract void print (Graphics2D parG);

 70|

 71|

 72|   /**

 73|    * Method: printChilds <p>

 74|    *

 75|    * @param parG a value of type Graphics2D

 76|    */

 77|   public void printChilds (Graphics2D parG) {

 78|

 79|      int i;

 80|      PFPrintObject tempPrintObject;

 81|

 82|      //--- Draw child objects

 83|      for (i = 0; i < printObjectCollection.size (); i++) {

 84|         tempPrintObject = (PFPrintObject) printObjectCollection.get (i);

 85|         tempPrintObject.print (parG);

 86|      }

 87|   }

 88|

 89|

 90|   /**

 91|    * Method: getDrawingOrigin <p>

 92|    *

 93|    * @return a value of type PFPoint

 94|    */

 95|   public PFPoint getDrawingOrigin () {

 96|

 97|      return (drawOrigin);

 98|

 99|   }

100|

101|

102|   /**

103|    * Method: getDrawingSize <p>

104|    *

105|    * @return a value of type PFSize

106|    */

107|   public PFSize getDrawingSize () {

108|

109|      return (drawSize);

110|

111|   }

112|

113|

114|

115|   /**

116|    * Method: computePositionAndSize <p>

117|    *

118|    */

119|   public void computePositionAndSize () {

120|

121|      PFPoint location;

122|      PFInchUnit parentCenter;

123|      PFInchUnit childCenter;

124|      PFInchUnit objectPosition;

125|      PFPoint parentLocation;

126|      PFSize parentSize;

127|

128|

129|      //--- Get parent infos

130|      if (isChild ()) {

131|         parentLocation = getParent ().getDrawingOrigin ();

132|         parentSize = getParent ().getDrawingSize ();

133|      }

134|      else {

135|         parentLocation = getPage ().getPrintableAreaOrigin ();

136|         parentSize = getPage ().getPrintableAreaSize ();

137|      }

138|

139|      //--- Validate relative mode and set the draw origin

140|      if (relativeMode) {

141|         drawOrigin = (PFPoint) position.clone ();

142|         drawOrigin.add (parentLocation);

143|      }

144|      else {

145|         drawOrigin = (PFPoint) position.clone ();

146|      }

147|

148|      //--- Initialiaze the size

149|      drawSize = (PFSize) size.clone ();

150|

151|      //--- Set the margins

152|      drawOrigin.setX (drawOrigin.getX ().add (leftMargin));

153|      drawOrigin.setY (drawOrigin.getY ().add (topMargin));

154|      drawSize.setWidth (drawSize.getWidth ().substract (leftMargin.add (rightMargin)));

155|      drawSize.setHeight (drawSize.getHeight ().substract (topMargin.add (bottomMargin)));

156|

157|

158|      //--- Compute the fills

159|      if (horizontalFill) {

160|         drawSize = new PFSize (parentSize.getWidth (), drawSize.getHeight ());

161|      }

162|

163|      if (verticalFill) {

164|         drawSize = new PFSize (drawSize.getWidth (), parentSize.getHeight ());

165|      }

166|

167|      //--- Compute vertical sticky options

168|      switch (verticalSticky) {

169|

170|         case STICKY_TOP:

171|            drawOrigin.setLocation (drawOrigin.getX (), parentLocation.getY ());

172|            break;

173|

174|         case STICKY_CENTER:

175|            parentCenter = (PFInchUnit) parentSize.getHeight ().divide (2.0);

176|            childCenter = (PFInchUnit) this.getSize ().getHeight ().divide (2.0);

177|            objectPosition = (PFInchUnit) parentCenter.substract (childCenter);

178|            drawOrigin.setLocation (drawOrigin.getX (), objectPosition.add (drawOrigin.getY ()));

179|            break;

180|

181|         case STICKY_BOTTOM:

182|            objectPosition = (PFInchUnit) parentSize.getHeight ().substract (this.getSize ().getHeight ());

183|            drawOrigin.setLocation (drawOrigin.getX (),

184|                                    objectPosition.add (drawOrigin.getY ()));

185|            break;

186|      }

187|

188|

189|      //--- Compute horizontal sticky

190|      switch (horizontalSticky) {

191|

192|         case STICKY_LEFT:

193|            drawOrigin.setLocation (drawOrigin.getX (),

194|                                    drawOrigin.getY ());

195|            break;

196|

197|         case STICKY_CENTER:

198|            parentCenter = (PFInchUnit) parentSize.getWidth ().divide (2.0);

199|            childCenter = (PFInchUnit) this.getSize ().getWidth ().divide (2.0);

200|            objectPosition = (PFInchUnit) parentCenter.substract (childCenter);

201|            drawOrigin.setLocation (objectPosition.add (drawOrigin.getX ()),

202|                                    drawOrigin.getY ());

203|            break;

204|

205|         case STICKY_RIGHT:

206|            objectPosition = (PFInchUnit) parentSize.getWidth ().substract (this.getSize ().getWidth ());

207|            drawOrigin.setLocation (objectPosition.add (drawOrigin.getX ()),

208|                                    drawOrigin.getY ());

209|            break;

210|      }

211|

212|   }

213|

214|

215|   /**

216|    * Method: setVerticalSticky <p>

217|    *

218|    * @param parSticky a value of type int

219|    */

220|   public void setVerticalSticky (int parSticky) {

221|

222|      verticalSticky = parSticky;

223|   }

224|

225|

226|   /**

227|    * Method: getVerticalSticky <p>

228|    *

229|    * @return a value of type int

230|    */

231|   public int getVerticalSticky () {

232|

233|      return (verticalSticky);

234|

235|   }

236|

237|

238|   /**

239|    * Method: setHorizontalSticky <p>

240|    *

241|    * @param parSticky a value of type int

242|    */

243|   public void setHorizontalSticky (int parSticky) {

244|

245|      horizontalSticky = parSticky;

246|

247|   }

248|

249|

250|   /**

251|    * Method: getHorizontalSticky <p>

252|    *

253|    * @return a value of type int

254|    */

255|   public int getHorizontalSticky () {

256|

257|      return (horizontalSticky);

258|

259|   }

260|

261|

262|   /**

263|    * Method: setHorizontalFill <p>

264|    *

265|    * @param parFill a value of type boolean

266|    */

267|   public void setHorizontalFill (boolean parFill) {

268|

269|      horizontalFill = parFill;

270|

271|   }

272|

273|

274|   /**

275|    * Method: getHorizontalFill <p>

276|    *

277|    * @return a value of type boolean

278|    */

279|   public boolean getHorizontalFill () {

280|

281|      return (horizontalFill);

282|

283|   }

284|

285|

286|   /**

287|    * Method: setVerticalFill <p>

288|    *

289|    * @param parFill a value of type boolean

290|    */

291|   public void setVerticalFill (boolean parFill) {

292|

293|      verticalFill = parFill;

294|

295|   }

296|

297|

298|   /**

299|    * Method: getVerticalFill <p>

300|    *

301|    * @return a value of type boolean

302|    */

303|   public boolean getVerticalFill () {

304|

305|      return (verticalFill);

306|

307|   }

308|

309|

310|   /**

311|    * Method: add <p>

312|    *

313|    * @param parPrintObject a value of type PFPrintObject

314|    */

315|   public void add (PFPrintObject parPrintObject) {

316|

317|      printObjectCollection.add (parPrintObject);

318|      parPrintObject.setParent (this);

319|

320|   }

321|

322|

323|   /**

324|    * Method: setParent <p>

325|    *

326|    * @param parPrintObject a value of type PFPrintObject

327|    */

328|   private void setParent (PFPrintObject parPrintObject) {

329|

330|      parentObject = parPrintObject;

331|

332|   }

333|

334|

335|   /**

336|    * Method: getParent <p>

337|    *

338|    * @return a value of type PFPrintObject

339|    */

340|   public PFPrintObject getParent () {

341|

342|      return (parentObject);

343|

344|   }

345|

346|

347|   /**

348|    * Method: isChild <p>

349|    *

350|    * @return a value of type boolean

351|    */

352|   public boolean isChild () {

353|

354|      if (parentObject != null)

355|         return (true);

356|      else

357|         return (false);

358|

359|   }

360|

361|

362|   /**

363|    * Method: remove <p>

364|    *

365|    * @param parPrintObject a value of type PFPrintObject

366|    */

367|   public void remove (PFPrintObject parPrintObject) {

368|

369|      printObjectCollection.remove (parPrintObject);

370|      parPrintObject.setParent (null);

371|

372|   }

373|

374|

375|   /**

376|    * Method: setPage <p>

377|    *

378|    * @param parPage a value of type PFPage

379|    */

380|   public void setPage (PFPage parPage) {

381|

382|      int i;

383|      PFPrintObject tempPrintObject;

384|

385|      //--- Set this object parent page

386|      page = parPage;

387|

388|      //--- Set childs parent page

389|      for (i = 0; i < printObjectCollection.size (); i++) {

390|         tempPrintObject = (PFPrintObject) printObjectCollection.get (i);

391|         tempPrintObject.setPage (parPage);

392|      }

393|   }

394|

395|

396|   /**

397|    * Method: getPage <p>

398|    *

399|    * @return a value of type PFPage

400|    */

401|   public PFPage getPage () {

402|

403|      return (page);

404|

405|   }

406|

407|

408|   /**

409|    * Method: setPosition <p>

410|    *

411|    * @param parPosition a value of type PFPoint

412|    */

413|   public void setPosition (PFPoint parPosition) {

414|

415|

416|      position = parPosition;

417|

418|   }

419|

420|

421|   /**

422|    * Method: getPosition <p>

423|    *

424|    * @return a value of type PFPoint

425|    */

426|   public PFPoint getPosition () {

427|

428|      return (position);

429|

430|   }

431|

432|

433|   /**

434|    * Method: setSize <p>

435|    *

436|    * @param parSize a value of type PFSize

437|    */

438|   public void setSize (PFSize parSize) {

439|

440|

441|      size = parSize;

442|

443|   }

444|

445|

446|   /**

447|    * Method: getSize <p>

448|    *

449|    * @return a value of type PFSize

450|    */

451|   public PFSize getSize () {

452|

453|      return (size);

454|

455|   }

456|

457|

458|   /**

459|    * Method: setWidth <p>

460|    *

461|    * @param parWidth a value of type PFUnit

462|    */

463|   public void setWidth (PFUnit parWidth) {

464|

465|      size.setWidth (parWidth);

466|

467|   }

468|

469|

470|   /**

471|    * Method: setHeight <p>

472|    *

473|    * @param parHeight a value of type PFUnit

474|    */

475|   public void setHeight (PFUnit parHeight) {

476|

477|      size.setHeight (parHeight);

478|

479|   }

480|

481|

482|   /**

483|    * Method: setRelativeMode <p>

484|    *

485|    * @param parMode a value of type boolean

486|    */

487|   public void setRelativeMode (boolean parMode) {

488|

489|      relativeMode = parMode;

490|   }

491|

492|

493|   /**

494|    * Method: setLeftMargin <p>

495|    *

496|    * @param parLeftMargin a value of type PFUnit

497|    */

498|   public void setLeftMargin (PFUnit parLeftMargin) {

499|

500|

501|      leftMargin = parLeftMargin;

502|

503|   }

504|

505|

506|   /**

507|    * Method: getLeftMargin <p>

508|    *

509|    * @return a value of type PFSize

510|    */

511|   public PFUnit getLeftMargin () {

512|

513|      return (leftMargin);

514|

515|   }

516|

517|

518|   /**

519|    * Method: setRightMargin <p>

520|    *

521|    * @param parRightMargin a value of type PFUnit

522|    */

523|   public void setRightMargin (PFUnit parRightMargin) {

524|

525|      rightMargin = parRightMargin;

526|

527|   }

528|

529|

530|   /**

531|    * Method: getRightMargin <p>

532|    *

533|    * @return a value of type PFSize

534|    */

535|   public PFUnit getRightMargin () {

536|

537|

538|      return (rightMargin);

539|

540|   }

541|

542|

543|   /**

544|    * Method: setTopMargin <p>

545|    *

546|    * @param parTopMargin a value of type PFUnit

547|    */

548|   public void setTopMargin (PFUnit parTopMargin) {

549|

550|      topMargin = parTopMargin;

551|

552|   }

553|

554|

555|   /**

556|    * Method: getTopMargin <p>

557|    *

558|    * @return a value of type PFUnit

559|    */

560|   public PFUnit getTopMargin () {

561|

562|      return (topMargin);

563|

564|

565|   }

566|

567|

568|

569|   /**

570|    * Method: setBottomMargin <p>

571|    *

572|    * @param parBottomMargin a value of type PFUnit

573|    */

574|   public void setBottomMargin (PFUnit parBottomMargin) {

575|

576|

577|      bottomMargin = parBottomMargin;

578|

579|

580|   }

581|

582|

583|   /**

584|    * Method: getBottomMargin <p>

585|    *

586|    * @return a value of type PFUnit

587|    */

588|   public PFUnit getBottomMargin () {

589|

590|

591|      return (bottomMargin);

592|

593|   }

594|

595|

596|}// PFPrintObject