summaryrefslogtreecommitdiff
path: root/android/source/src/java/org/libreoffice/ToolbarController.java
blob: e4a55dd424b6ba031a788d04c86b3d3732d5e64d (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
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice;

import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

/**
 * Controls the changes to the toolbar.
 */
public class ToolbarController implements Toolbar.OnMenuItemClickListener {
    private static final String LOGTAG = ToolbarController.class.getSimpleName();
    private final Toolbar mToolbarTop;

    private final LibreOfficeMainActivity mContext;
    private final Menu mMainMenu;

    public ToolbarController(LibreOfficeMainActivity context, Toolbar toolbarTop) {
        mToolbarTop = toolbarTop;
        mContext = context;

        mToolbarTop.inflateMenu(R.menu.main);
        mToolbarTop.setOnMenuItemClickListener(this);
        switchToViewMode();

        mMainMenu = mToolbarTop.getMenu();
    }

    private void disableMenuItem(final int menuItemId, final boolean disabled) {
        LOKitShell.getMainHandler().post(new Runnable() {
            public void run() {
                MenuItem menuItem = mMainMenu.findItem(menuItemId);
                if (menuItem != null) {
                    menuItem.setEnabled(!disabled);
                } else {
                    Log.e(LOGTAG, "MenuItem not found.");
                }
            }
        });
    }

    /**
     * Change the toolbar to edit mode.
     */
    void switchToEditMode() {
        if (!LOKitShell.isEditingEnabled())
            return;

        // Ensure the change is done on UI thread
        LOKitShell.getMainHandler().post(new Runnable() {
            @Override
            public void run() {
                mMainMenu.setGroupVisible(R.id.group_edit_actions, true);
                mToolbarTop.setNavigationIcon(R.drawable.ic_check);
                mToolbarTop.setLogo(null);

            }
        });
    }

    /**
     * Change the toolbar to view mode.
     */
    void switchToViewMode() {
        if (!LOKitShell.isEditingEnabled())
            return;

        // Ensure the change is done on UI thread
        LOKitShell.getMainHandler().post(new Runnable() {
            @Override
            public void run() {
                mMainMenu.setGroupVisible(R.id.group_edit_actions, false);
                mToolbarTop.setNavigationIcon(R.drawable.lo_icon);
                mToolbarTop.setLogo(null);
            }
        });
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_keyboard:
                mContext.showSoftKeyboard();
                break;
            case R.id.action_format:
                mContext.showFormattingToolbar();
                break;
            case R.id.action_about:
                mContext.showAbout();
                return true;
            case R.id.action_save:
                mContext.saveDocument();
                return true;
            case R.id.action_parts:
                mContext.openDrawer();
                return true;
            case R.id.action_settings:
                mContext.showSettings();
                return true;
            case R.id.action_search:
                mContext.showSearchToolbar();
                return true;
            case R.id.action_undo:
                LOKitShell.sendEvent(new LOEvent(LOEvent.UNO_COMMAND, ".uno:Undo"));
                return true;
            case R.id.action_redo:
                LOKitShell.sendEvent(new LOEvent(LOEvent.UNO_COMMAND, ".uno:Redo"));
                return true;
        }
        return false;
    }

    void setupToolbars() {
        if (mContext.usesTemporaryFile()) {
            disableMenuItem(R.id.action_save, true);
            Toast.makeText(mContext, mContext.getString(R.string.temp_file_saving_disabled), Toast.LENGTH_LONG).show();
        }
        mMainMenu.findItem(R.id.action_parts).setVisible(mContext.isDrawerEnabled());
    }

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */