|
21 | 21 | import com.oracle.bmc.model.BmcException; |
22 | 22 | import java.awt.event.ActionEvent; |
23 | 23 | import java.awt.event.ActionListener; |
| 24 | +import java.io.IOException; |
24 | 25 | import java.util.ArrayList; |
| 26 | +import java.util.HashMap; |
25 | 27 | import java.util.List; |
| 28 | +import java.util.Map; |
26 | 29 | import java.util.Optional; |
27 | 30 | import java.util.logging.Level; |
28 | 31 | import java.util.logging.Logger; |
29 | 32 | import java.util.stream.Collectors; |
30 | 33 | import org.netbeans.modules.cloud.oracle.OCIManager; |
31 | 34 | import org.netbeans.modules.cloud.oracle.OCIProfile; |
| 35 | +import org.netbeans.modules.cloud.oracle.actions.DownloadWalletDialog.WalletInfo; |
32 | 36 | import org.netbeans.modules.cloud.oracle.compartment.CompartmentItem; |
33 | 37 | import org.netbeans.modules.cloud.oracle.compartment.CompartmentNode; |
34 | 38 | import org.netbeans.modules.cloud.oracle.database.DatabaseItem; |
|
38 | 42 | import org.openide.DialogDescriptor; |
39 | 43 | import org.openide.DialogDisplayer; |
40 | 44 | import org.openide.NotifyDescriptor; |
| 45 | +import org.openide.NotifyDescriptor.ComposedInput.Callback; |
| 46 | +import org.openide.NotifyDescriptor.QuickPick; |
41 | 47 | import org.openide.NotifyDescriptor.QuickPick.Item; |
42 | 48 | import org.openide.awt.ActionID; |
43 | 49 | import org.openide.awt.ActionReference; |
44 | 50 | import org.openide.awt.ActionReferences; |
45 | 51 | import org.openide.awt.ActionRegistration; |
| 52 | +import org.openide.util.Exceptions; |
46 | 53 | import org.openide.util.NbBundle; |
47 | 54 |
|
48 | 55 | /** |
|
65 | 72 | "AddADB=Add Oracle Autonomous DB", |
66 | 73 | "SelectTenancy=Select Tenancy", |
67 | 74 | "SelectCompartment=Select Compartment", |
68 | | - "SelectDatabase=Select Database" |
| 75 | + "SelectDatabase=Select Compartment or Database", |
| 76 | + "EnterUsername=Enter Username", |
| 77 | + "EnterPassword=Enter Password" |
69 | 78 | }) |
70 | 79 | public class AddADBAction implements ActionListener { |
71 | 80 | private static final Logger LOGGER = Logger.getLogger(AddADBAction.class.getName()); |
72 | 81 |
|
| 82 | + private static final String DB = "db"; //NOI18N |
| 83 | + private static final String USERNAME = "username"; //NOI18N |
| 84 | + private static final String PASSWORD = "password"; //NOI18N |
73 | 85 |
|
74 | 86 | @Override |
75 | 87 | public void actionPerformed(ActionEvent e) { |
76 | | - List<TenancyItem> tenancies = new ArrayList<>(); |
77 | | - for (OCIProfile p : OCIManager.getDefault().getConnectedProfiles()) { |
78 | | - p.getTenancy().ifPresent(tenancies::add); |
79 | | - } |
80 | | - Optional<TenancyItem> selectedTenancy = chooseOneItem(tenancies, Bundle.SelectTenancy()); |
81 | | - |
82 | | - Optional<CompartmentItem> selectedCompartment = Optional.empty(); |
83 | | - |
84 | | - if (!selectedTenancy.isPresent()) { |
85 | | - return; |
86 | | - } |
87 | | - |
88 | | - List<CompartmentItem> compartments = CompartmentNode.getCompartments().apply(selectedTenancy.get()); |
89 | | - selectedCompartment = chooseOneItem(compartments, Bundle.SelectCompartment()); |
90 | | - DatabaseItem selectedDatabase = null; |
| 88 | + Map<String, Object> result = new HashMap<> (); |
91 | 89 |
|
92 | | - if (selectedCompartment.isPresent()) { |
93 | | - while(selectedDatabase == null) { |
94 | | - OCIItem item = chooseCopartmentOrDb(selectedCompartment.get()); |
95 | | - if (item == null) { |
96 | | - return; |
97 | | - } |
98 | | - if (item instanceof DatabaseItem) { |
99 | | - selectedDatabase = (DatabaseItem) item; |
100 | | - } |
101 | | - if (item instanceof CompartmentItem) { |
102 | | - selectedCompartment = Optional.of((CompartmentItem) item); |
| 90 | + NotifyDescriptor.ComposedInput ci = new NotifyDescriptor.ComposedInput(Bundle.AddADB(), 3, new Callback() { |
| 91 | + Map<Integer, List> values = new HashMap<> (); |
| 92 | + |
| 93 | + @Override |
| 94 | + public NotifyDescriptor createInput(NotifyDescriptor.ComposedInput input, int number) { |
| 95 | + if (number == 1) { |
| 96 | + List<TenancyItem> tenancies = new ArrayList<>(); |
| 97 | + for (OCIProfile p : OCIManager.getDefault().getConnectedProfiles()) { |
| 98 | + p.getTenancy().ifPresent(tenancies::add); |
| 99 | + } |
| 100 | + String title; |
| 101 | + if (tenancies.size() == 1) { |
| 102 | + values.put(1, getCompartmentsAndDbs(tenancies.get(0))); |
| 103 | + title = Bundle.SelectCompartment(); |
| 104 | + } else { |
| 105 | + values.put(1, tenancies); |
| 106 | + title = Bundle.SelectTenancy(); |
| 107 | + } |
| 108 | + return createQuickPick(values.get(1), title); |
| 109 | + } else { |
| 110 | + NotifyDescriptor prev = input.getInputs()[number - 2]; |
| 111 | + OCIItem prevItem = null; |
| 112 | + if (prev instanceof NotifyDescriptor.QuickPick) { |
| 113 | + Optional<String> selected = ((QuickPick) prev).getItems().stream().filter(item -> item.isSelected()).map(item -> item.getLabel()).findFirst(); |
| 114 | + if (selected.isPresent()) { |
| 115 | + Optional<? extends OCIItem> ti = values.get(number - 1).stream().filter(t -> ((OCIItem) t).getName().equals(selected.get())).findFirst(); |
| 116 | + if (ti.isPresent()) { |
| 117 | + prevItem = ti.get(); |
| 118 | + } |
| 119 | + } |
| 120 | + if (prevItem instanceof DatabaseItem) { |
| 121 | + result.put(DB, prevItem); |
| 122 | + return new NotifyDescriptor.InputLine(Bundle.EnterUsername(), Bundle.EnterUsername()); |
| 123 | + } |
| 124 | + values.put(number, getCompartmentsAndDbs(prevItem)); |
| 125 | + input.setEstimatedNumberOfInputs(input.getEstimatedNumberOfInputs() + 1); |
| 126 | + return createQuickPick(values.get(number), Bundle.SelectDatabase()); |
| 127 | + } else if (prev instanceof NotifyDescriptor.PasswordLine) { |
| 128 | + result.put(PASSWORD, ((NotifyDescriptor.PasswordLine) prev).getInputText()); |
| 129 | + return null; |
| 130 | + } else if (prev instanceof NotifyDescriptor.InputLine) { |
| 131 | + String username = ((NotifyDescriptor.InputLine) prev).getInputText(); |
| 132 | + if (username == null || username.trim().isEmpty()) { |
| 133 | + return prev; |
| 134 | + } |
| 135 | + result.put(USERNAME, username); |
| 136 | + return new NotifyDescriptor.PasswordLine(Bundle.EnterPassword(), Bundle.EnterPassword()); |
| 137 | + } |
| 138 | + return null; |
103 | 139 | } |
104 | 140 | } |
105 | | - } |
106 | | - if (selectedDatabase != null) { |
107 | | - DownloadWalletAction action = new DownloadWalletAction(selectedDatabase); |
108 | | - action.actionPerformed(null); |
| 141 | + |
| 142 | + }); |
| 143 | + if (DialogDescriptor.OK_OPTION == DialogDisplayer.getDefault().notify(ci)) { |
| 144 | + try { |
| 145 | + DatabaseItem selectedDatabase = (DatabaseItem) result.get(DB); |
| 146 | + DownloadWalletAction action = new DownloadWalletAction(selectedDatabase); |
| 147 | + WalletInfo info = new WalletInfo( |
| 148 | + DownloadWalletDialog.getWalletsDir().getAbsolutePath(), |
| 149 | + AbstractPasswordPanel.generatePassword(), |
| 150 | + (String) result.get(USERNAME), |
| 151 | + ((String) result.get(PASSWORD)).toCharArray()); |
| 152 | + action.addConnection(info); |
| 153 | + } catch (IOException ex) { |
| 154 | + Exceptions.printStackTrace(ex); |
| 155 | + } |
109 | 156 | } |
110 | 157 | } |
111 | 158 |
|
112 | | - private <T extends OCIItem> Optional<T> chooseOneItem(List<T> ociItems, String title) { |
113 | | - Optional<T> result = Optional.empty(); |
114 | | - if (ociItems.size() == 1) { |
115 | | - result = Optional.of(ociItems.get(0)); |
116 | | - } else if (ociItems.size() > 0) { |
117 | | - List<Item> items = ociItems.stream() |
118 | | - .map(tenancy -> new Item(tenancy.getName(), tenancy.getDescription())) |
119 | | - .collect(Collectors.toList()); |
120 | | - NotifyDescriptor.QuickPick qp = new NotifyDescriptor.QuickPick(title, title, items, false); |
121 | | - if (DialogDescriptor.OK_OPTION == DialogDisplayer.getDefault().notify(qp)) { |
122 | | - Optional<String> selected = qp.getItems().stream().filter(item -> item.isSelected()).map(item -> item.getLabel()).findFirst(); |
123 | | - if (selected.isPresent()) { |
124 | | - result = ociItems.stream().filter(t -> t.getName().equals(selected.get())).findFirst(); |
125 | | - } |
126 | | - |
127 | | - } |
128 | | - } |
129 | | - return result; |
| 159 | + private <T extends OCIItem> NotifyDescriptor.QuickPick createQuickPick(List<T> ociItems, String title) { |
| 160 | + |
| 161 | + List<Item> items = ociItems.stream() |
| 162 | + .map(tenancy -> new Item(tenancy.getName(), tenancy.getDescription())) |
| 163 | + .collect(Collectors.toList()); |
| 164 | + return new NotifyDescriptor.QuickPick(title, title, items, false); |
130 | 165 | } |
131 | 166 |
|
132 | | - |
133 | | - private OCIItem chooseCopartmentOrDb(CompartmentItem compartment) { |
| 167 | + private List<OCIItem> getCompartmentsAndDbs(OCIItem parent) { |
134 | 168 | List<OCIItem> items = new ArrayList<> (); |
135 | 169 | try { |
136 | | - items.addAll(DatabaseNode.getDatabases().apply(compartment)); |
| 170 | + if (parent instanceof CompartmentItem) { |
| 171 | + items.addAll(DatabaseNode.getDatabases().apply((CompartmentItem) parent)); |
| 172 | + } |
137 | 173 | } catch (BmcException e) { |
138 | 174 | LOGGER.log(Level.SEVERE, "Unable to load compartment list", e); // NOI18N |
139 | 175 | } |
140 | | - items.addAll(CompartmentNode.getCompartments().apply(compartment)); |
141 | | - return chooseOneItem(items, Bundle.SelectDatabase()).orElseGet(() -> null); |
| 176 | + items.addAll(CompartmentNode.getCompartments().apply(parent)); |
| 177 | + return items; |
142 | 178 | } |
143 | 179 |
|
144 | 180 | } |
0 commit comments