summaryrefslogtreecommitdiff
path: root/libjava/classpath/java/awt/JobAttributes.java
blob: e84901d33b1347659c6628388b8a4e0a095cdb4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* JobAttributes.java --
   Copyright (C) 2002, 2005  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.awt;

import gnu.java.lang.CPStringBuilder;

/**
 * Needs documentation...
 *
 * @author Eric Blake (ebb9@email.byu.edu)
 * @since 1.3
 * @status updated to 1.4, lacks documentation
 */
public final class JobAttributes implements Cloneable
{
  public static final class DefaultSelectionType extends AttributeValue
  {
    private static final String[] NAMES = { "all", "range", "selection" };
    public static final DefaultSelectionType ALL
      = new DefaultSelectionType(0);
    public static final DefaultSelectionType RANGE
      = new DefaultSelectionType(1);
    public static final DefaultSelectionType SELECTION
      = new DefaultSelectionType(2);
    private DefaultSelectionType(int value)
    {
      super(value, NAMES);
    }
  } // class DefaultSelectionType

  public static final class DestinationType extends AttributeValue
  {
    private static final String[] NAMES = { "file", "printer" };
    public static final DestinationType FILE = new DestinationType(0);
    public static final DestinationType PRINTER = new DestinationType(1);
    private DestinationType(int value)
    {
      super(value, NAMES);
    }
  } // class DestinationType

  public static final class DialogType extends AttributeValue
  {
    private static final String[] NAMES = { "common", "native", "none" };
    public static final DialogType COMMON = new DialogType(0);
    public static final DialogType NATIVE = new DialogType(1);
    public static final DialogType NONE = new DialogType(2);
    private DialogType(int value)
    {
      super(value, NAMES);
    }
  } // class DialogType

  public static final class MultipleDocumentHandlingType
    extends AttributeValue
  {
    private static final String[] NAMES = {
      "separate-documents-collated-copies",
      "separate-documents-uncollated-copies"
    };
    public static final MultipleDocumentHandlingType
      SEPARATE_DOCUMENTS_COLLATED_COPIES
      = new MultipleDocumentHandlingType(0);
    public static final MultipleDocumentHandlingType
      SEPARATE_DOCUMENTS_UNCOLLATED_COPIES
      = new MultipleDocumentHandlingType(1);
    private MultipleDocumentHandlingType(int value)
    {
      super(value, NAMES);
    }
  } // class MultipleDocumentHandlingType

  public static final class SidesType extends AttributeValue
  {
    private static final String[] NAMES
      = { "one-sided", "two-sided-long-edge", "two-sided-short-edge" };
    public static final SidesType ONE_SIDED = new SidesType(0);
    public static final SidesType TWO_SIDED_LONG_EDGE = new SidesType(1);
    public static final SidesType TWO_SIDED_SHORT_EDGE = new SidesType(2);
    private SidesType(int value)
    {
      super(value, NAMES);
    }
  } // class SidesType

  private int copies;
  private DefaultSelectionType selection;
  private DestinationType destination;
  private DialogType dialog;
  private String filename;
  private int maxPage;
  private int minPage;
  private MultipleDocumentHandlingType multiple;
  private int[][] pageRanges; // null for default value
  private int fromPage; // 0 for default value
  private int toPage; // 0 for default value
  private String printer;
  private SidesType sides;

  public JobAttributes()
  {
    copies = 1;
    selection = DefaultSelectionType.ALL;
    destination = DestinationType.PRINTER;
    dialog = DialogType.NATIVE;
    maxPage = Integer.MAX_VALUE;
    minPage = 1;
    multiple
      = MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES;
    sides = SidesType.ONE_SIDED;
  }

  public JobAttributes(JobAttributes attr)
  {
    set(attr);
  }

  public JobAttributes(int copies, DefaultSelectionType selection,
                       DestinationType destination, DialogType dialog,
                       String filename, int max, int min,
                       MultipleDocumentHandlingType multiple,
                       int[][] pageRanges, String printer, SidesType sides)
  {
    if (copies <= 0 || selection == null || destination == null
        || dialog == null || max < min || min <= 0 || multiple == null
        || sides == null)
      throw new IllegalArgumentException();
    this.copies = copies;
    this.selection = selection;
    this.destination = destination;
    this.dialog = dialog;
    this.filename = filename;
    maxPage = max;
    minPage = min;
    this.multiple = multiple;
    setPageRanges(pageRanges);
    this.printer = printer;
    this.sides = sides;
  }

  public Object clone()
  {
    return new JobAttributes(this);
  }

  public void set(JobAttributes attr)
  {
    copies = attr.copies;
    selection = attr.selection;
    destination = attr.destination;
    dialog = attr.dialog;
    filename = attr.filename;
    maxPage = attr.maxPage;
    minPage = attr.minPage;
    multiple = attr.multiple;
    pageRanges = (int[][]) attr.pageRanges.clone();
    printer = attr.printer;
    sides = attr.sides;
    fromPage = attr.fromPage;
    toPage = attr.toPage;
  }

  public int getCopies()
  {
    return copies;
  }

  public void setCopies(int copies)
  {
    if (copies <= 0)
      throw new IllegalArgumentException();
    this.copies = copies;
  }

  public void setCopiesToDefault()
  {
    copies = 1;
  }

  public DefaultSelectionType getDefaultSelection()
  {
    return selection;
  }

  public void setDefaultSelection(DefaultSelectionType selection)
  {
    if (selection == null)
      throw new IllegalArgumentException();
    this.selection = selection;
  }

  public DestinationType getDestination()
  {
    return destination;
  }

  public void setDestination(DestinationType destination)
  {
    if (destination == null)
      throw new IllegalArgumentException();
    this.destination = destination;
  }

  public DialogType getDialog()
  {
    return dialog;
  }

  public void setDialog(DialogType dialog)
  {
    if (dialog == null)
      throw new IllegalArgumentException();
    this.dialog = dialog;
  }

  public String getFileName()
  {
    return filename;
  }

  public void setFileName(String filename)
  {
    this.filename = filename;
  }

  public int getFromPage()
  {
    return fromPage != 0 ? fromPage
      : pageRanges != null ? pageRanges[0][0]
      : toPage != 0 ? toPage : minPage;
  }

  public void setFromPage(int fromPage)
  {
    if (fromPage < minPage || (fromPage > toPage && toPage != 0)
        || fromPage > maxPage)
      throw new IllegalArgumentException();
    if (pageRanges == null)
      this.fromPage = fromPage;
  }

  public int getMaxPage()
  {
    return maxPage;
  }

  public void setMaxPage(int maxPage)
  {
    if (maxPage < minPage)
      throw new IllegalArgumentException();
    this.maxPage = maxPage;
    if (maxPage < fromPage)
      fromPage = maxPage;
    if (maxPage < toPage)
      toPage = maxPage;
    if (pageRanges != null)
      {
        int i = pageRanges.length - 1;
        while (i >= 0 && maxPage < pageRanges[i][1])
          i--;
        if (maxPage >= pageRanges[++i][0])
          pageRanges[i++][1] = maxPage;
        if (i == 0)
          pageRanges = null;
        else if (i < pageRanges.length)
          {
            int[][] tmp = new int[i][];
            System.arraycopy(pageRanges, 0, tmp, 0, i);
            pageRanges = tmp;
          }
      }
  }

  public int getMinPage()
  {
    return minPage;
  }

  public void setMinPage(int minPage)
  {
    if (minPage <= 0 || minPage > maxPage)
      throw new IllegalArgumentException();
    this.minPage = minPage;
    if (minPage > toPage)
      toPage = minPage;
    if (minPage > fromPage)
      fromPage = minPage;
    if (pageRanges != null)
      {
        int size = pageRanges.length;
        int i = 0;
        while (i < size && minPage > pageRanges[i][0])
          i++;
        if (minPage <= pageRanges[i - 1][1])
          pageRanges[--i][0] = minPage;
        if (i == size)
          pageRanges = null;
        else if (i > 0)
          {
            int[][] tmp = new int[size - i][];
            System.arraycopy(pageRanges, i, tmp, 0, size - i);
            pageRanges = tmp;
          }
      }
  }

  public MultipleDocumentHandlingType getMultipleDocumentHandling()
  {
    return multiple;
  }

  public void setMultipleDocumentHandling
    (MultipleDocumentHandlingType multiple)
  {
    if (multiple == null)
      throw new IllegalArgumentException();
    this.multiple = multiple;
  }

  public void setMultipleDocumentHandlingToDefault()
  {
    multiple
      = MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES;
  }

  public int[][] getPageRanges()
  {
    if (pageRanges == null)
      return new int[][] { { getFromPage(), getToPage() } };
    // Perform a deep clone, so user code cannot affect original arrays.
    int i = pageRanges.length;
    int[][] result = new int[i][];
    while (--i >= 0)
      result[i] = (int[]) pageRanges[i].clone();
    return result;
  }

  public void setPageRanges(int[][] pageRanges)
  {
    int size = pageRanges == null ? 0 : pageRanges.length;
    if (size == 0)
      throw new IllegalArgumentException();
    while (--size >= 0)
      {
        int[] range = pageRanges[size];
        if (range == null || range.length != 2
            || range[0] < minPage || range[1] < range[0] || range[1] > maxPage
            || (size != 0 && range[0] <= pageRanges[size - 1][1]))
          throw new IllegalArgumentException();
      }
    size = pageRanges.length;
    if (fromPage > 0 && pageRanges[0][0] > fromPage)
      fromPage = pageRanges[0][0];
    if (toPage > 0 && pageRanges[size - 1][1] < toPage)
      toPage = pageRanges[size - 1][1];
    this.pageRanges = new int[size][];
    while (--size >= 0)
      this.pageRanges[size] = (int[]) pageRanges[size].clone();
  }

  public String getPrinter()
  {
    return printer;
  }

  public void setPrinter(String printer)
  {
    this.printer = printer;
  }

  public SidesType getSides()
  {
    return sides;
  }

  public void setSides(SidesType sides)
  {
    if (sides == null)
      throw new IllegalArgumentException();
    this.sides = sides;
  }

  public void setSidesToDefault()
  {
    sides = SidesType.ONE_SIDED;
  }

  public int getToPage()
  {
    return toPage != 0 ? toPage
      : pageRanges != null ? pageRanges[pageRanges.length - 1][1]
      : fromPage != 0 ? fromPage : maxPage;
  }

  public void setToPage(int toPage)
  {
    if (toPage < minPage || (fromPage > toPage && fromPage != 0)
        || toPage > maxPage)
      throw new IllegalArgumentException();
    if (pageRanges == null)
      this.toPage = toPage;
  }

  public boolean equals(Object o)
  {
    if (this == o)
      return true;
    if (! (o instanceof JobAttributes))
      return false;
    JobAttributes ja = (JobAttributes) o;
    if (copies != ja.copies || selection != ja.selection
        || destination != ja.destination || dialog != ja.dialog
        || ! filename.equals(ja.filename) || maxPage != ja.maxPage
        || minPage != ja.minPage || multiple != ja.multiple
        || fromPage != ja.fromPage || toPage != ja.toPage
        || ! printer.equals(ja.printer) || sides != ja.sides
        || (pageRanges == null) != (ja.pageRanges == null))
      return false;
    if (pageRanges != ja.pageRanges)
      for (int i = pageRanges.length; --i >= 0; )
        if (pageRanges[i][0] != ja.pageRanges[i][0]
            || pageRanges[i][1] != ja.pageRanges[i][1])
          return false;
    return true;
  }

  public int hashCode()
  {
    int hash = (selection.value << 6) ^ (destination.value << 5)
      ^ (dialog.value << 3) ^ (multiple.value << 2) ^ sides.value
      ^ (filename == null ? 0 : filename.hashCode())
      ^ (printer == null ? 0 : printer.hashCode());
    // The effect of the above fields on the hashcode match the JDK. However,
    // I am unable to reverse engineer the effect of the fields listed below,
    // so I am using my own implementation. Note that this still satisfies
    // the general contract of hashcode, it just doesn't match the JDK.
    hash ^= (copies << 27) ^ (maxPage << 22) ^ (minPage << 17);
    if (pageRanges == null)
      hash ^= (getFromPage() << 13) ^ (getToPage() << 8);
    else
      for (int i = pageRanges.length; --i >= 0; )
        hash ^= (pageRanges[i][0] << 13) ^ (pageRanges[i][1] << 8);
    return hash;
  }

  public String toString()
  {
    CPStringBuilder s = new CPStringBuilder("copies=").append(copies)
      .append(",defaultSelection=").append(selection).append(",destination=")
      .append(destination).append(",dialog=").append(dialog)
      .append(",fileName=").append(filename).append(",fromPage=")
      .append(getFromPage()).append(",maxPage=").append(maxPage)
      .append(",minPage=").append(minPage)
      .append(",multiple-document-handling=").append(multiple)
      .append(",page-ranges=[");
    if (pageRanges == null)
      s.append(minPage).append(':').append(minPage).append(']');
    else
      for (int i = 0; i < pageRanges.length; i++)
        s.append(pageRanges[i][0]).append(':').append(pageRanges[i][1])
          .append(',');
    s.setLength(s.length() - 1);
    return s.append("],printer=").append(printer).append(",sides=")
      .append(sides).append(",toPage=").append(getToPage()).toString();
  }
} // class JobAttributes