]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape4a/PublisherFragment.java
Initial commit of shape4a v1.0 - shape for android.
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape4a / PublisherFragment.java
1 /**
2  * 
3  *      This file is part of shape4a.
4  *
5  *  shape4a is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  shape4a is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with shape4a.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 package org.ocera.orte.shape4a;
20
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.content.DialogInterface;
24 import android.content.res.Configuration;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.support.v4.app.DialogFragment;
28 import android.support.v4.app.Fragment;
29 import android.view.LayoutInflater;
30 import android.view.Menu;
31 import android.view.MenuInflater;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.LinearLayout;
36 import android.widget.SeekBar;
37
38 import org.ocera.orte.DomainApp;
39
40 /**
41  * Fragment containing publishing workspace.
42  * 
43  * @author jiri hubacek <jiri.hubacek@gmail.com>
44  * @version %I%, %G%
45  * @since 1.0
46  */
47 public class PublisherFragment extends Fragment {
48         private static final int REDRAW_INTERVAL = 50;
49         private static final int STRENGTH_MAX = 5;
50         
51         private View rootView;
52         private PublisherView publisherView;
53         
54         private Handler handler = new Handler();
55         private Runnable redraw = new Runnable()
56         {
57                 @Override
58                 public void run()
59                 {
60                         publisherView.countShapes();
61                         publisherView.invalidate();
62                         handler.postDelayed(this, REDRAW_INTERVAL);
63                 }
64         };
65         
66         NewDialogFragment newDialogFragment = new NewDialogFragment();
67         PublisherSettingsDialogFragment publisherSettingsDialogFragment = new PublisherSettingsDialogFragment();
68         
69         private DomainApp appDomain;
70         
71         /**
72          * Constructor just stores appDomain variable.
73          * 
74          * @param appDomain
75          * @since 1.0
76          */
77         public PublisherFragment(DomainApp appDomain)
78         {
79                 this.appDomain = appDomain;
80         }
81
82         /**
83          * When screen orientation is changed,
84          * scale recalculation is needed.
85          * 
86          * @since 1.0
87          */
88         @Override
89         public void onConfigurationChanged(Configuration newConfig)
90         {
91                 for (PublisherShape s : this.publisherView.shapes) {
92                         s.setScale(getActivity().getWindowManager().getDefaultDisplay().getWidth(), getActivity().getWindowManager().getDefaultDisplay().getHeight());
93                 }
94         }
95         
96         /**
97          * Setting up workspace, adding publisherView
98          * and timer (handler) for drawing objects.
99          * 
100          * @since 1.0
101          */
102         @Override
103         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
104         {
105                 this.rootView = inflater.inflate(R.layout.fragment_publisher, container, false);
106                 this.publisherView = (PublisherView) this.rootView.findViewById(R.id.publisher_view);
107                 this.handler.postDelayed(redraw, REDRAW_INTERVAL);
108                 
109                 return this.rootView;
110         }
111         
112         /**
113          * Creating menu.
114          * 
115          * @since 1.0
116          */
117         @Override
118         public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
119         {
120                 inflater.inflate(R.menu.publisher, menu);
121         }
122         
123         /**
124          * All created publishers are in menu.
125          * 
126          * @since 1.0
127          */
128         @Override
129         public void onPrepareOptionsMenu(Menu  menu)
130         {
131                 for (PublisherShape shape : publisherView.shapes) {
132                         menu.add(0, publisherView.shapes.indexOf(shape), 20 + publisherView.shapes.indexOf(shape), "#" + publisherView.shapes.indexOf(shape) + " " + shape.getColorName() + " " + shape.getShapeName());
133                 }
134         }
135         
136         /**
137          * Select settings of publishers
138          * or add new publisher.
139          * 
140          * @since 1.0
141          */
142         @Override
143         public boolean onOptionsItemSelected(MenuItem item)
144         {
145                 // Handle action bar item clicks here. The action bar will
146                 // automatically handle clicks on the Home/Up button, so long
147                 // as you specify a parent activity in AndroidManifest.xml.
148                 int id = item.getItemId();
149                 
150                 if (id >= 0 && id < publisherView.shapes.size()) {
151                         publisherSettingsDialogFragment.setId(id);
152                         publisherSettingsDialogFragment.show(getFragmentManager(), "dialog");
153                 } else {
154                         switch (id) {
155                         case R.id.action_new:
156                                 newDialogFragment.show(getFragmentManager(), "dialog");
157                                 break;
158                         }
159                 }
160                 return super.onOptionsItemSelected(item);
161         }
162         
163         /**
164          * Dialog for adding new publisher
165          * throw action bar button +.
166          * 
167          * @since 1.0
168          */
169         public class NewDialogFragment extends DialogFragment
170         {
171                 /*
172                  * Selection of colors is badly solved,
173                  * it should be altered..
174                  * 
175                  */
176                 private String[] colors = new String[5];
177                 
178                 /**
179                  * In constructor, make array
180                  * of colors.
181                  * 
182                  * @since 1.0
183                  */
184                 public NewDialogFragment()
185                 {
186                         // Texts should be done better way,
187                         // like centralized in strings.xml
188                         
189                         colors[0] = "Blue";
190                         colors[1] = "Green";
191                         colors[2] = "Red";
192                         colors[3] = "Black";
193                         colors[4] = "Yellow";
194                 }
195                 
196                 /**
197                  * Creating dialog for new publisher.
198                  * 
199                  * @since 1.0
200                  */
201                 @Override
202                 public Dialog onCreateDialog(Bundle savedInstanceState)
203                 {
204                         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
205                         builder.setTitle(R.string.dialog_newPublisher)
206                                 .setItems(colors, new DialogInterface.OnClickListener()
207                                 {
208                                         @Override
209                                         public void onClick(DialogInterface dialog, int which)
210                                         {
211                                                 // new publisher
212                                                 
213                                                 publisherView.addShape(which, appDomain);
214
215                                                 for (PublisherShape s : publisherView.shapes) {
216                                                         s.setScale(getActivity().getWindowManager().getDefaultDisplay().getWidth(), getActivity().getWindowManager().getDefaultDisplay().getHeight());
217                                                 }
218                                                 
219                                                 getActivity().supportInvalidateOptionsMenu();
220                                         }
221                                 })
222                                 .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener()
223                                 {
224                                         @Override
225                                         public void onClick(DialogInterface dialog, int which)
226                                         {       
227                                         }
228                                 });
229                         return builder.create();
230                         
231                 }
232         }
233         
234         /**
235          * When click on shape in Action Bar menu.
236          * 
237          * @since 1.0
238          */
239         public class PublisherSettingsDialogFragment extends DialogFragment
240         {
241                 private int shapeId;
242                 private int shapeStrength;
243                 private SeekBar seekStrength;
244                 
245                 /**
246                  * No constructor needed.
247                  * 
248                  * @since 1.0
249                  */
250                 public PublisherSettingsDialogFragment()
251                 {}
252                 
253                 /**
254                  * Set wanted publisher's dialog.
255                  * 
256                  * @param i Index of publisher.
257                  * @since 1.0
258                  */
259                 public void setId(int i)
260                 {
261                         this.shapeId = i;
262                         this.shapeStrength = publisherView.shapes.get(this.shapeId).getStrength();
263                 }
264                 
265                 /**
266                  * Creating dialog for publisher's settings.
267                  * 
268                  * @since 1.0
269                  */
270                 @Override
271                 public Dialog onCreateDialog(Bundle savedInstanceState)
272                 {
273                         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
274                         builder.setTitle("#" + this.shapeId + " " + publisherView.shapes.get(this.shapeId).getColorName() + " " + publisherView.shapes.get(this.shapeId).getShapeName() + " strength:")
275                                 .setView(seekBar())
276                                 .setPositiveButton(R.string.dialog_OK, new DialogInterface.OnClickListener()
277                                 {
278                                         @Override
279                                         public void onClick(DialogInterface dialog, int which)
280                                         {
281                                                 publisherView.shapes.get(shapeId).setStrength(shapeStrength);
282                                         }
283                                 })
284                                 .setNeutralButton(R.string.dialog_delete, new DialogInterface.OnClickListener()
285                                 {       
286                                         @Override
287                                         public void onClick(DialogInterface dialog, int which) {
288                                                 publisherView.shapes.get(shapeId).killMe();
289                                                 publisherView.shapes.remove(shapeId);
290                                                 getActivity().supportInvalidateOptionsMenu();
291                                         }
292                                 })
293                                 .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener()
294                                 {
295                                         @Override
296                                         public void onClick(DialogInterface dialog, int which)
297                                         {       
298                                         }
299                                 });
300                         return builder.create();
301                 }
302                 
303                 /**
304                  * SeekBar for setting-up strength of publishers.
305                  * 
306                  * @return Layout of SeekBar.
307                  * @since 1.0
308                  */
309                 private View seekBar(){
310                         LinearLayout l = new LinearLayout(getActivity());
311                         l.setOrientation(LinearLayout.VERTICAL);
312                         
313                         seekStrength = new SeekBar(getActivity());
314                         seekStrength.setMax(STRENGTH_MAX);
315                         seekStrength.setProgress(shapeStrength);
316                         seekStrength.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
317                         {
318                                 @Override
319                                 public void onStopTrackingTouch(SeekBar seekBar) {
320                                 }
321                                 
322                                 @Override
323                                 public void onStartTrackingTouch(SeekBar seekBar) {
324                                 }
325                                 
326                                 @Override
327                                 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
328                                 {
329                                         shapeStrength = progress;
330                                 }
331                         });
332                         
333                         l.addView(seekStrength);
334                         return l;
335                 }
336         }
337 }