Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2f9cf5f2f2f65030aa7c0c1200d7770862ba9aa8 (plain) (blame)
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
/*
 * Copyright (c) 2013 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Christian W. Damus (CEA LIST) - initial API and implementation
 */
package org.eclipse.net4j.util.confirmation;

import org.eclipse.net4j.util.factory.ProductCreationException;

import java.util.Set;

/**
 * A provider of user confirmation of some action.
 * 
 * @author Christian W. Damus (CEA LIST)
 * @since 3.4
 */
public interface IConfirmationProvider
{
  /**
   * Requests confirmation of some action/operation/consequence pertaining to a
   * {@code subject} and described by a {@code message}.  Any of the non-empty
   * set of {@code acceptable} responses may be returned, and the requester
   * may optionally provide a {@code suggestion} of a suitable/safe default
   * answer.
   */
  public Confirmation confirm(String subject, String message, Set<Confirmation> acceptable, Confirmation suggestion);

  public boolean isInteractive();

  /**
   * @author Christian W. Damus (CEA LIST)
   * @since 3.4
   */
  public static abstract class Factory extends org.eclipse.net4j.util.factory.Factory
  {
    public static final String PRODUCT_GROUP = "org.eclipse.net4j.util.confirmationProviders"; //$NON-NLS-1$

    public static final String DEFAULT_TYPE = "default"; //$NON-NLS-1$

    public static final String INTERACTIVE_TYPE = "interactive"; //$NON-NLS-1$

    public Factory(String type)
    {
      super(PRODUCT_GROUP, type);
    }

    /**
     * @author Christian W. Damus (CEA LIST)
     * @since 3.4
     */
    public static class Default extends Factory
    {
      public Default()
      {
        super(DEFAULT_TYPE);
      }

      public Object create(String description) throws ProductCreationException
      {
        return new ConfirmationProvider();
      }

      private static final class ConfirmationProvider implements IConfirmationProvider
      {
        public boolean isInteractive()
        {
          return false;
        }

        public Confirmation confirm(String subject, String message, Set<Confirmation> acceptable,
            Confirmation suggestion)
        {
          // Just return the suggestion, or else the greatest of the acceptable set
          return suggestion != null ? suggestion : max(acceptable);
        }

        private Confirmation max(Set<Confirmation> confirmations)
        {
          Confirmation[] all = Confirmation.values();
          for (int i = all.length - 1; i >= 0; i--)
          {
            if (confirmations.contains(all[i]))
            {
              return all[i];
            }
          }

          return null;
        }
      }
    }
  }

  /**
   * @author Christian W. Damus (CEA LIST)
   * @since 3.4
   */
  public static interface Provider
  {
    public IConfirmationProvider getConfirmationProvider();
  }
}

Back to the top